这可能是一个愚蠢的问题,我对C++和编程很新.我想了解几个STL容器的使用,考虑到这一点,我想知道使用std :: set vs使用矢量或地图的优点是什么?我似乎无法找到这个问题的明确答案.我注意到集合使用地图,但为什么不总是使用地图或总是使用集合.而是提供了两个非常相似的容器.提前致谢.
我希望存储一个大的d维点矢量(d固定和小:<10).
如果我定义一个Pointas vector<int>,我认为a vector<Point>会在每个位置存储指向Point的指针.
但是,如果将a定义Point为固定大小的对象,如:
std::tuple<int,int,...,int>或者std::array<int, d>,程序是否会将所有点存储在连续的内存中,还是会保留额外的间接级别?
如果答案是数组避免额外的间接,那么在扫描时,这会对性能(缓存利用局部性)产生很大影响vector<Point>吗?
我在我的代码中声明了以下内容
vector <const A> mylist;
Run Code Online (Sandbox Code Playgroud)
我得到以下编译错误 -
new_allocator.h:75: error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const \[with _Tp = const A]' and `_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const A]' cannot be overloaded
Run Code Online (Sandbox Code Playgroud)
但如果宣布 -
vector <A> mylist;
Run Code Online (Sandbox Code Playgroud)
我的代码编译.
在这种情况下不允许使用const吗?
我在这里复制我的代码供大家参考 -
#include <iostream>
#include <vector>
using namespace std;
class A
{
public:
A () {cout << "default constructor\n";}
A (int i): m(i) {cout << "non-default constructor\n";}
private:
int m;
};
int main (void)
{
vector<const A> mylist;
mylist.push_back(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在以下代码中:
std::vector<int> var;
for (int i = 0; i < var.size(); i++);
Run Code Online (Sandbox Code Playgroud)
size()成员函数是为每个循环迭代调用的,还是仅调用一次?
设置std::vector<int>范围的最佳方法是什么,例如3到16之间的所有数字?
考虑以下程序:
#include <vector>
#include <iostream>
class A {
int x;
public:
A(int n) noexcept : x(n) { std::cout << "ctor with value\n"; }
A(const A& other) noexcept : x(other.x) { std::cout << "copy ctor\n"; }
A(A&& other) noexcept : x(other.x) { std::cout << "move ctor\n"; }
~A() { std::cout << "dtor\n"; } // (*)
};
int main()
{
std::vector<A> v;
v.emplace_back(123);
v.emplace_back(456);
}
Run Code Online (Sandbox Code Playgroud)
如果我运行该程序,我会得到(GodBolt):
ctor with value
ctor with value
move ctor
dtor
dtor
dtor
Run Code Online (Sandbox Code Playgroud)
……这符合我的预期。但是,如果在线(*)我将析构函数标记为可能抛出,那么我会 …
/* bar.h */
class bar{
/* standard stuff omitted */
std::vector<my_obj*> foo;
};
/* bar.cpp */
bar::bar(){
// foo = new std::vector<my_obj*>(); <-- why don't I need this line??
foo.push_back(new my_obj());
}
Run Code Online (Sandbox Code Playgroud)
为什么这个代码工作,即使我们没有为foo :: vector的新实例分配foo?
在以下情况下,我发现了一些非常奇怪的行为(在 clang 和 GCC 上)。我有一个向量,nodes一个元素,一个 class 的实例Node。然后我调用一个函数nodes[0],Node为向量添加一个新的。添加新节点时,调用对象的字段将重置!然而,一旦功能完成,它们似乎又恢复正常。
我相信这是一个最小的可重复示例:
#include <iostream>
#include <vector>
using namespace std;
struct Node;
vector<Node> nodes;
struct Node{
int X;
void set(){
X = 3;
cout << "Before, X = " << X << endl;
nodes.push_back(Node());
cout << "After, X = " << X << endl;
}
};
int main() {
nodes = vector<Node>();
nodes.push_back(Node());
nodes[0].set();
cout << "Finally, X = " << nodes[0].X << endl;
}
Run Code Online (Sandbox Code Playgroud)
哪些输出
#include …Run Code Online (Sandbox Code Playgroud) 当我clear()在a上使用std::vector它时,它应该销毁它中的所有元素vector,而不是它.
示例代码:
vector<double> temp1(4);
cout << temp1.size() << std::endl;
temp1.clear();
cout << temp1.size() << std::endl;
temp1[2] = 343.5; // I should get segmentation fault here ....
cout << "Printing..... " << temp1[2] << endl;
cout << temp1.size() << std::endl;
Run Code Online (Sandbox Code Playgroud)
现在,我应该在尝试访问已清除的向量时遇到分段错误,但是它会填充那里的值(据我说这是非常错误的)
结果如下:
4
0
Printing..... 343.5
0
Run Code Online (Sandbox Code Playgroud)
这是正常的吗?这是一个非常难以发现的错误,它基本上杀了我的代码数月.
之间有什么区别?
auto x = vector<int>();
Run Code Online (Sandbox Code Playgroud)
和
vector<int> x;
Run Code Online (Sandbox Code Playgroud)
这两个声明是否相等,或者运行时复杂度是否有所不同?