我正在学习来自 python 背景的 c++。
我想知道有没有办法将项目附加到 C++ 中的列表中?
myList = []
for i in range(10):
myList.append(i)
在 c++ 中是否有类似的东西可以对数组执行?
我正在学习来自 python 背景的 c++。
我想知道有没有办法将项目附加到 C++ 中的列表中?
myList = []
for i in range(10):
myList.append(i)
在 c++ 中是否有类似的东西可以对数组执行?
你需要一个向量,做这样的事情:
#include <vector>
void funct() {
std::vector<int> myList;
for(int i = 0; i < 10; i++)
myList.push_back(10);
}
有关更多信息,请参阅http://cplusplus.com/reference/stl/vector/。
对于列表使用std::list::push_back
如果您正在寻找与 C++ 等效的数组,您应该使用std::vector
向量也有一个std::vector::push_back方法
你应该使用向量:
vector<int> v;
for(int i = 0; i < 10; i++)
v.push_back(i);
如果你使用std::vector
,有一种push_back
方法可以做同样的事情。
列表具有 push_back 方法。
myList.push_back(myElement);
它将 myElement 推到 myList 的末尾。与 Python 的 list.append 相同。