我有这个功能:
template<typename T>
void Inventory::insertItem(std::vector<T>& v, const T& x)
{
std::vector<T>::iterator it; // doesn't compile
for(it=v.begin(); it<v.end(); ++it)
{
if(x <= *it) // if the insertee is alphabetically less than this index
{
v.insert(it, x);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和g ++给出了这些错误:
src/Item.hpp: In member function ‘void
yarl::item::Inventory::insertItem(std::vector<T, std::allocator<_CharT> >&, const T&)’:
src/Item.hpp:186: error: expected ‘;’ before ‘it’
src/Item.hpp:187: error: ‘it’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
它必须是简单的东西,但在盯着它十分钟后我找不到任何错误.别人看到了吗?
std::vector和之间有什么区别std::stack?
显然,向量可以删除集合中的项目(虽然比列表慢得多),而堆栈则构建为仅限LIFO的集合.
但是,对于最终项目操作,堆栈更快吗?它是链表还是动态重新分配的数组?
我找不到很多关于堆栈的信息,但是如果我正确地描绘它们(它们类似于实际的线程堆栈;推送,弹出等等 - 以及该top()方法)那么它们似乎非常适合窗口堆叠管理.
我正在阅读 C++ 之旅(第二版),我遇到了这段代码(6.2 参数化类型):
\ntemplate<typename T>\nT* end(Vector<T>& x)\n{\n \xc2\xa0\xc2\xa0 \xc2\xa0return x.size() ? &x[0]+x.size() : nullptr; \xc2\xa0 \xc2\xa0 //\xc2\xa0pointer to one-past-last element \n}\nRun Code Online (Sandbox Code Playgroud)\n我不明白为什么我们使用&x[0]+x.size()而不是&x[x.size()]. 这是否意味着我们取出第一个元素的地址x并添加到该数字上x.size()字节?
我的代码有问题吗?
std::vector<int[2]> weights;
int weight[2] = {1,2};
weights.push_back(weight);
Run Code Online (Sandbox Code Playgroud)
它无法编译,请帮助解释原因:
no matching function for call to ‘std::vector<int [2], std::allocator<int [2]> >::push_back(int*&)’
Run Code Online (Sandbox Code Playgroud) 我有一个std::vector.我检查它的大小是6,但是当我尝试访问vec[6]以检查它是否会出错时,我得到的不是错误而是一些数字.它不应该出错吗?
编辑:类似于:
struct Element
{
std::vector<double> face;
};
int main()
{
Element elm;
.... // insert 6 elements into elm.face
std::cout << elm.face.size() << std::endl; // answer is 6
std::cout << elm.face[6] << std::endl; // answer is some number
}
Run Code Online (Sandbox Code Playgroud) 请有人解释为什么以下代码不能使用clang 3.5进行编译.
报告的错误是"算法中没有可行的重载"="."
std::vector<const std::string> m_messages;
std::vector<const std::string>::iterator iter;
...
if (iter != m_messages.end())
{
m_messages.erase(iter); // compilation error
}
Run Code Online (Sandbox Code Playgroud)
如果我声明m_messages为:std::vector<std::string> m_messages;那么它编译好了.
另外,有什么区别:
std::vector<const std::string> m_messages;
Run Code Online (Sandbox Code Playgroud)
和
std::vector<std::string> m_messages;
Run Code Online (Sandbox Code Playgroud)
TIA.
我有一个std向量与这样的元素:
[0 , 1 , 2 , 0 , 2 , 1 , 0 , 0 , 188 , 220 , 0 , 1 , 2 ]
Run Code Online (Sandbox Code Playgroud)
找到并复制在此向量中仅出现一次的元素的最有效方法是什么,不包括强力O(n ^ 2)算法?在这种情况下,新列表应包含[188, 220]
使用以下代码时,我对链接器错误感到困惑:
// static_const.cpp -- complete code
#include <vector>
struct Elem {
static const int value = 0;
};
int main(int argc, char *argv[]) {
std::vector<Elem> v(1);
std::vector<Elem>::iterator it;
it = v.begin();
return it->value;
}
Run Code Online (Sandbox Code Playgroud)
但是,这在链接时失败 - 不知何故,它需要有一个静态const"值"的符号.
$ g++ static_const.cpp
/tmp/ccZTyfe7.o: In function `main':
static_const.cpp:(.text+0x8e): undefined reference to `Elem::value'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这与-O1或更好的编译很好; 但对于更复杂的情况,它仍然失败.我使用的是gcc版本4.4.4 20100726(Red Hat 4.4.4-13).
任何想法我的代码可能有什么问题?
我有许多具有不同签名的回调函数.理想情况下,我想将它们放在一个向量中,并根据某些条件调用适当的一个.
例如
void func1(const std::string& value);
void func2(const std::string& value, int min, int max);
const std::vector<std::function<void(std::string)>> functions
{
func1,
func2,
};
Run Code Online (Sandbox Code Playgroud)
我意识到上述情况是不可能的,但我想知道是否有任何我应该考虑的替代方案.我还没有找到任何东西,我已经尝试过,std::bind但没有成功实现我想要的东西.
这样的事情可能吗?
这是一个打算:
a)接受int的向量
b)对于输入向量中的每个int,追加此int的逆
先决条件:无
postconditions:返回vector的size()是2*输入向量的大小.
请注意,矢量是就地修改的.
在变换期间,此函数是否严格定义了与迭代器失效相关的行为?
是否有更好/更简洁/更健壮的方式来编写它?
std::vector<int> append_negatives(std::vector<int> v)
{
v.reserve(v.size() * 2);
std::transform(begin(v), end(v),
back_inserter(v),
[](auto&& x) { return -x; });
return v;
}
Run Code Online (Sandbox Code Playgroud)