Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ vectors fun.
#include <iostream>
#include <vector>
using namespace std;
struct test {
int a;
long b;
};
int main() {
unsigned int expectedSize = 100;
vector<test> data;
data.reserve(expectedSize);
auto it = data.begin();
for (unsigned int i = 0; i < expectedSize; i++) {
// Do something
test t;
t.a = i;
// Put in vector - no error at all
(*it) = t;
it++;
}
cout << "expectedSize = " << expectedSize << endl;
cout << "data.size() = " << data.size() << endl;
cout << "data.capacity() = " << data.capacity() << endl;
// Dependent on implementation
cout << endl << "data[2891] = " << data[2891].a << endl;
// cout << endl << "data[2892] = " << data[2892].a << endl; // Gives segfault
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.