push-back

How do I pass multiple ints into a vector at once?

筅森魡賤 提交于 2019-11-30 05:52:30
问题 Currently when I have to use vector.push_back() multiple times. The code I'm currently using is std::vector<int> TestVector; TestVector.push_back(2); TestVector.push_back(5); TestVector.push_back(8); TestVector.push_back(11); TestVector.push_back(14); Is there a way to only use vector.push_back() once and just pass multiple values into the vector? 回答1: Try pass array to vector: int arr[] = {2,5,8,11,14}; std::vector<int> TestVector(arr, arr+5); You could always call std::vector::assign to

c++ garbage values in vector of pointer

南笙酒味 提交于 2019-11-29 16:23:21
When I do: for(i=0; i<size; i++){ //create objectA here vectorA.push_back(objectA); pvectorA.push_back(&vectorA[i]); } some elements of pvectorA is garbage. However when I do: for(i=0; i<size; i++){ //create objectA here vectorA.push_back(objectA); } for(i=0; i<size; i++){ pvectorA.push_back(&vectorA[i]); } Everything is okay. Why is it happens? Read the documentation of std::vector::push_back First the description: Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container

GDB wrong values for vector.size()

霸气de小男生 提交于 2019-11-29 14:53:26
A simple vector.push_back() causes some error in my code: #include <vector> using namespace std; int main(int argc, const char *argv[]) { vector<unsigned> stack; stack.push_back(1); stack.push_back(1); //stack.size() becomes 467369971 after this stack.push_back(1); stack.push_back(1); ... more push_back()s ... return 0; } I'm using GDB to check its behavior... and the weird thing is that stack.size() goes wrong after the second push_back(). It becomes 467369971! What may be wrong? I'm on Win7 64-bit, and I'm using MinGW with G++ 4.7.0 Below is the output of GDB: (gdb) n 5 std::vector<unsigned>

vector::push_back vs vector::operator[]

流过昼夜 提交于 2019-11-29 00:43:31
Below in c++ program, include<iostream> #include<vector> using namespace std; int main() { vector<int> numbers; numbers.push_back(2); numbers.push_back(10); numbers.push_back(5); numbers.push_back(3); numbers.push_back(7); numbers[3] = 8; numbers[5] = 11; for(int i=0; i<numbers.size(); ++i) { cout<<" "<<numbers[i]; } } see it on ideone . here, numbers[3] is working but numbers[5] . It looks like, vector::operator[] doesn't increase the size of vector like vector::push_back. so, is this the only difference between these two or something else is there ? push_back creates a new element on the

c++ garbage values in vector of pointer

前提是你 提交于 2019-11-28 11:24:21
问题 When I do: for(i=0; i<size; i++){ //create objectA here vectorA.push_back(objectA); pvectorA.push_back(&vectorA[i]); } some elements of pvectorA is garbage. However when I do: for(i=0; i<size; i++){ //create objectA here vectorA.push_back(objectA); } for(i=0; i<size; i++){ pvectorA.push_back(&vectorA[i]); } Everything is okay. Why is it happens? 回答1: Read the documentation of std::vector::push_back First the description: Adds a new element at the end of the vector, after its current last

C++ reference changes when push_back new element to std::vector

半世苍凉 提交于 2019-11-28 09:26:54
I am not sure what to make of this - please tell me what's wrong with the code below. I modified my code to reduce it to the simplest terms. There is a std::vector with a bunch of MyNode objects. The first step is to get a constant reference to one of the data elements of one of these nodes (Data m_data) - in the example below, there is only one node before the 2nd node is inserted as seen below: const cv::Data& currData = m_nodesVector[currIndex].GetData(); MyNode node(...); m_nodesVector.push_back(node); At exactly the vector::push_back call, the value of currData changes!! I just don't get

GDB wrong values for vector.size()

不羁的心 提交于 2019-11-28 08:42:48
问题 A simple vector.push_back() causes some error in my code: #include <vector> using namespace std; int main(int argc, const char *argv[]) { vector<unsigned> stack; stack.push_back(1); stack.push_back(1); //stack.size() becomes 467369971 after this stack.push_back(1); stack.push_back(1); ... more push_back()s ... return 0; } I'm using GDB to check its behavior... and the weird thing is that stack.size() goes wrong after the second push_back(). It becomes 467369971! What may be wrong? I'm on Win7

How do I pass multiple ints into a vector at once?

半腔热情 提交于 2019-11-27 19:36:01
Currently when I have to use vector.push_back() multiple times. The code I'm currently using is std::vector<int> TestVector; TestVector.push_back(2); TestVector.push_back(5); TestVector.push_back(8); TestVector.push_back(11); TestVector.push_back(14); Is there a way to only use vector.push_back() once and just pass multiple values into the vector? Try pass array to vector: int arr[] = {2,5,8,11,14}; std::vector<int> TestVector(arr, arr+5); You could always call std::vector::assign to assign array to vector, call std::vector::insert to add multiple arrays. If you use C++11, you can try: std:

Why emplace_back is faster than push_back?

醉酒当歌 提交于 2019-11-27 17:31:22
I thought that emplace_back would be the winner, when doing something like this: v.push_back(myClass(arg1, arg2)); because emplace_back would construct the object immediately in the vector, while push_back , would first construct an anonymous object and then would copy it to the vector. For more see this question. Google also gives this and this questions. I decided to compare them for a vector that would be filled by integers. Here is the experiment code: #include <iostream> #include <vector> #include <ctime> #include <ratio> #include <chrono> using namespace std; using namespace std::chrono;

Is it safe to push_back an element from the same vector?

a 夏天 提交于 2019-11-27 00:00:52
vector<int> v; v.push_back(1); v.push_back(v[0]); If the second push_back causes a reallocation, the reference to the first integer in the vector will no longer be valid. So this isn't safe? vector<int> v; v.push_back(1); v.reserve(v.size() + 1); v.push_back(v[0]); This makes it safe? It looks like http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#526 addressed this problem (or something very similar to it) as a potential defect in the standard: 1) Parameters taken by const reference can be changed during execution of the function Examples: Given std::vector v: v.insert(v.begin(), v