Skip to content

Instantly share code, notes, and snippets.

@kantoniak
Created March 20, 2017 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kantoniak/7f8e887e871423462218cc4da4262b07 to your computer and use it in GitHub Desktop.
Save kantoniak/7f8e887e871423462218cc4da4262b07 to your computer and use it in GitHub Desktop.
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