草庐IT

stdvector

全部标签

c++ - 创建一个固定大小的 std::vector 并写入元素

在C++中,我希望分配一个固定大小(但大小在运行时确定)std::vector,然后写入该vector中的元素。这是我正在使用的代码:intb=30;conststd::vectortest(b);int&a=test[3];但是,这给了我一个编译器(MSVC2010Pro)错误:errorC2440:'initializing':cannotconvertfrom'constint'to'int&'.Conversionlosesqualifiers.我对const的理解是,它使类的所有成员变量都成为常量。例如,以下工作正常:classmyvec{public:myvec(intnu

c++ - 创建一个固定大小的 std::vector 并写入元素

在C++中,我希望分配一个固定大小(但大小在运行时确定)std::vector,然后写入该vector中的元素。这是我正在使用的代码:intb=30;conststd::vectortest(b);int&a=test[3];但是,这给了我一个编译器(MSVC2010Pro)错误:errorC2440:'initializing':cannotconvertfrom'constint'to'int&'.Conversionlosesqualifiers.我对const的理解是,它使类的所有成员变量都成为常量。例如,以下工作正常:classmyvec{public:myvec(intnu

c++ - 为什么我不能在即时窗口中索引 std::vector?

所以,我有一个vectorstd::vectorlines.我把这个vector填满,然后就可以访问它了std::stringtemp=lines[0];但是,在即时窗口中,两个lines[0]-error:overloadedoperatornotfound和lines.at(0)-error:symbolisambiguous根本不工作。在c++中使用即时窗口是否有技巧。我主要来自C#背景,一切都很好(并且我在即时窗口中有智能感知)。我没想到C++会很棒,但我认为它适用于整数以外的东西。谁能告诉我我做错了什么?谢谢。编辑:我应该清楚,在即时窗口中没有什么真正起作用的,这只是一个简化

c++ - 为什么我不能在即时窗口中索引 std::vector?

所以,我有一个vectorstd::vectorlines.我把这个vector填满,然后就可以访问它了std::stringtemp=lines[0];但是,在即时窗口中,两个lines[0]-error:overloadedoperatornotfound和lines.at(0)-error:symbolisambiguous根本不工作。在c++中使用即时窗口是否有技巧。我主要来自C#背景,一切都很好(并且我在即时窗口中有智能感知)。我没想到C++会很棒,但我认为它适用于整数以外的东西。谁能告诉我我做错了什么?谢谢。编辑:我应该清楚,在即时窗口中没有什么真正起作用的,这只是一个简化

c++ - std::vector 与 std::stack

std::vector和std::stack有什么区别?很明显,vector可以删除集合中的项目(尽管比列表慢得多),而堆栈被构建为仅限LIFO的集合。但是,对于最终项目的操作,堆栈是否更快?是链表还是动态重新分配的数组?我找不到关于堆栈的太多信息,但如果我正确地描绘了它们(它们类似于实际的线程堆栈;push、pop等-以及那个top()方法),那么它们似乎非常适合窗口堆叠管理。 最佳答案 stack不是容器;它是一个容器适配器。它有一个vector、deque或类似的容器,将其存储为实际保存元素的成员。记住:它被声明为:templ

c++ - std::vector 与 std::stack

std::vector和std::stack有什么区别?很明显,vector可以删除集合中的项目(尽管比列表慢得多),而堆栈被构建为仅限LIFO的集合。但是,对于最终项目的操作,堆栈是否更快?是链表还是动态重新分配的数组?我找不到关于堆栈的太多信息,但如果我正确地描绘了它们(它们类似于实际的线程堆栈;push、pop等-以及那个top()方法),那么它们似乎非常适合窗口堆叠管理。 最佳答案 stack不是容器;它是一个容器适配器。它有一个vector、deque或类似的容器,将其存储为实际保存元素的成员。记住:它被声明为:templ

c++ - 'std::vector<T>::iterator it ;' doesn' t 编译

我有这个功能:templatevoidInventory::insertItem(std::vector&v,constT&x){std::vector::iteratorit;//doesn'tcompilefor(it=v.begin();it而g++给出了这些错误:src/Item.hpp:Inmemberfunction‘voidyarl::item::Inventory::insertItem(std::vector>&,constT&)’:src/Item.hpp:186:error:expected‘;’before‘it’src/Item.hpp:187:error:‘

c++ - 'std::vector<T>::iterator it ;' doesn' t 编译

我有这个功能:templatevoidInventory::insertItem(std::vector&v,constT&x){std::vector::iteratorit;//doesn'tcompilefor(it=v.begin();it而g++给出了这些错误:src/Item.hpp:Inmemberfunction‘voidyarl::item::Inventory::insertItem(std::vector>&,constT&)’:src/Item.hpp:186:error:expected‘;’before‘it’src/Item.hpp:187:error:‘

c++ - 用一个范围(一对迭代器)初始化 std::array

如何初始化std::array从一个范围(由一对迭代器定义)?类似这样的:vectorv;...//IknowvhasexactlyNelements(e.g.Ijustcalledv.resize(N))//NowIwantainitializedwiththoseelementsarraya(???);//whattoputhere?我以为array会有一个构造函数采用一对迭代器,这样我就可以做arraya(v.begin(),v.end()),但它似乎根本没有构造函数!我知道我可以copyvector到数组中,但我宁愿直接用vector内容初始化数组,而不是先默认构造它。我该怎么

c++ - 用一个范围(一对迭代器)初始化 std::array

如何初始化std::array从一个范围(由一对迭代器定义)?类似这样的:vectorv;...//IknowvhasexactlyNelements(e.g.Ijustcalledv.resize(N))//NowIwantainitializedwiththoseelementsarraya(???);//whattoputhere?我以为array会有一个构造函数采用一对迭代器,这样我就可以做arraya(v.begin(),v.end()),但它似乎根本没有构造函数!我知道我可以copyvector到数组中,但我宁愿直接用vector内容初始化数组,而不是先默认构造它。我该怎么