Einfache Frage zum Thema Vectoren
-
Ich hab ein struct aller->
struct foo { int a, b; }
und mache später ein:
vector<foo> myVector;
wie spreche ich nun a bzw. b richtig per push_back() an?
myVector.a.push_back( 1 );
isses ja nicht:(
-
struct foo { int a, b; }; vector <foo> myVec; foo temp; temp.a = 1; temp.b = 2; myVec.push_back(temp); temp.a = 3; temp.b = 4; myVec.push_back(temp);
Du musst die gesammte Struktur haben, sie befüllen und mit push_back in den Vector kopieren.
-
Verpasse Deiner Struktur entweder einen geeigneten Konstruktor (siehe unten) oder erstelle einfach ein temporäres Objekt das Du dann per push_back() übergibst.
struct foo { foo(const int A, const int B) :a(A), b(B) {} int a, b; }; // [...] std::vector<foo> bar; bar.push_back(foo(1, 2));
Sowas in der Art sollte funktionieren.