Created
March 20, 2017 22:38
-
-
Save kantoniak/7f8e887e871423462218cc4da4262b07 to your computer and use it in GitHub Desktop.
C++ vectors fun.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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