c++ equivalent to python append method for lists
I'm learning c++ coming from a background in python.
I'm wondering is there 开发者_如何学Ca way to append items to a list in c++?
myList = []
for i in range(10):
myList.append(i)
Is there something like this in c++ that you can do to an array?
You need a vector, do something like this:
#include <vector>
void funct() {
std::vector<int> myList;
for(int i = 0; i < 10; i++)
myList.push_back(10);
}
See http://cplusplus.com/reference/stl/vector/ for more information.
For list Use std::list::push_back
If you are looking for a array equivalent of C++, You should use std::vector
vector also has a std::vector::push_back method
You should use vector:
vector<int> v;
for(int i = 0; i < 10; i++)
v.push_back(i);
If you use std::vector
, there's a push_back
method that does the same thing.
Lists have the push_back method.
myList.push_back(myElement);
It pushes myElement onto the end of myList. Same as Python's list.append.
精彩评论