Reputation: 113
I have defined struct with 3 UnicodeString values, so I can create struct variable like this:
someStruct x = {"value1","value2","value3"};
But what I want to do is a DynamicArray of my struct type and I want to add elements to this array without creating them earlier, but assigning struct values the same moment I add an array element.
I tried to do it this way
DynamicArray<someStruct> arrayOfSomeStruct;
arrayOfSomeStruct.Length = 1;
arrayOfSomeStruct[0] = {"value1","value2","value3"};
But it do not work this way. Would you help me with this?
EDIT: I have found the solution that works but I'm not fully happy with it:
arrayOfSomeStruct[0].atr1 = "value";
arrayOfSomeStruct[0].atr2 = "value";
arrayOfSomeStruct[0].atr3 = "value";
Upvotes: 0
Views: 122
Reputation: 21
Try to use vectors from the standard library
Vectors are sequence containers representing arrays that can change in size.
You can try the following code :
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct SomeStruct{
string str1;
string str2;
string str3;
};
int main()
{
vector< SomeStruct > someStructVector; //Create a vector of SomeStruct
someStructVector.push_back( {"str1 ", "str2 ", "str3"} ); //adds {"str1 ", "str2 ", "str3"} to the vector
for( auto ss : someStructVector )//Access the elements of the vector
cout << ss.str1 << ss.str2 << ss.str3 << endl;
return 0;
}
Upvotes: 2