Understanding Vectors and Data Structures

I want to store a series of data structures in a vector. I want to do something like the following.

1
2
std::vector <struc_t> structures;
structures.push_back(INSERT NEW STRUCTURE HERE);


I do not know the proper syntax for this and I cannot seem to find the solution online.
So far I have been able to accomplish this using the following code.

1
2
3
std::vector <struc_t> structures;
structures.resize(1);
structures[0].property1="propery";


This does not seem an efficient way to go about this because in order to add a new element one has to resize the vector based on the previous size.
closed account (S6k9GNh0)
It really depends on the size of the structure and the size of said vector.

If either is large, you'll probably want to look into alternatives such as a linked list.

If you're moving elements around consistently, use a linked list.

If you're removing and adding elements around consistently, do not use a vector.
Last edited on
What I am doing is taking data from an XML file and storing it in the data structure which I want to be able to access later for further computation. Once the data has been extracted from the XML file and added to the data structure which would then be placed in some kind of container I will not be adding or moving around elements. Is a linked list still the best container for this?
First of all I do not understand this code

1
2
3
std::vector <struc_t> structures;
structures.resize(1);
structures[0].property1="propery";


Why do you use resize with an empty vector? I think you could reserve memory as much as i can think it would br needed and after filling the vector you could free unused memory of the vector. For example

1
2
3
4
5
6
7
8
9
10
std::vector <struc_t> structures;
structures.reserve( 1000 );

while ( true )
{
   struc_t s;
   // filling the structure
   structures.push_back( s );
  // other code and testing the condition to leave the loop
}


As for selection of an appropriate container I think you should take into acccount searching of elements. Do you need a sequential searching or binary searching? Maybe you need std::multimap<std::string>, struc_t>?
I will be searching through the elements to find those elements with a specific property. Some elements will have a certain symetry (call it sym1) whereas others will have sym2. I will be dealing with only those that has sym2.
What is the type of the symetry? Maybe you should declare std::multimap as

std::maltimap<type_of_symmetry, std::string, some_predicate_for_symmetry>
Imagine this as an empty int vector:
{}

resize(10) does this:
{0000000000}

If you use push_back(4):
{0,0,0,0,0,0,0,0,0,0,4}

After
1
2
(vector name)[10]=0;
(vector name)[5]=4;


The vector looks like
{0,0,0,4,0,0,0,0,0,0,0}

If you use push_back(13) on an empty vector:
{13}

If you use resize(5) on that vector:
{13,0,0,0,0}

**EDIT** Corrected
Last edited on
If you use push_back(13) on an empty vector:
{13}

If you use resize(5) on that vector:
{0,0,0,0,0}


resize() does NOT clear the vector contents on expansion. It only deletes if you reduce the size.
Topic archived. No new replies allowed.