# Vector Struct

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-21-2011
Vector Struct
Hi, i am trying to create a vector a structure type

my code so far is

Code:

```        struct WEdge         {                 vector<int>From;        // an array for u points                 vector<int>To;        // an array for v points                 vector<int>Weight;        // an array for w points         };                 WEdge edges;         edges.From.push_back(1);         edges.To.push_back(2);         edges.Weight.push_back(5);```
this allows me to add elements, but seems weird considering other example on the internet.

but i think i shouldnt be doing it like this and should instead do something like

Code:

```struct WEdge         {                 int From;        // an array for u points                 int To;        // an array for v points                 int Weight;        // an array for w points         };                 vector<WEdge> edges; ..```
but when i do it this way i cant seem to add data using push_back method.

also i later need to be able to sort the data by the Weight, but move the from and to values along with the weight when sorting.

please clarify which method will be better for me.

----------------------------------------------------------
also, i have tried sorting using

Code:

`std::sort(edges.Weight.begin(), edges.Weight.end());`
but this only sorts the Weight and leaves the From and To values in the same place.

thanks
• 12-21-2011
VirtualAce
Separate the data points using more abstraction. Abstract out one data point in the structure and include all data relevant to one point. Then create a vector of said structures. What you have created there is going to be a massive memory hog since every data point can have a theoretical infinite amount of integers and you can have a theoretical infinite amount of those data points. Quite simply your data structure is ill-formed.

• 12-21-2011
thanks for the prompt response.
basically its to hold data for a minimum spanning tree algorithm.

so i need a from, to and a weight to make up one EDGE, so for example point 1 to point 2 with a weight of 10 to make up one Edge.
i want this to be all held in a vector so i can easily add and delete points.
I need to be able to sort the data after it is added by the weight, so that the EDGE with lowest weight is first.

thanks
• 12-21-2011
VirtualAce
Ok so what are the data types you are going to use for 'from' 'to' and 'weight'?

Quote:

i want this to be all held in a vector so i can easily add and delete points.
I need to be able to sort the data after it is added by the weight,
According to MSDN: (I am assuming you are using MSVC)
Code:

```Vectors allow constant time insertions and deletions at the end of the sequence. Inserting or deleting elements in the middle of a vector requires linear time. The performance of the deque Class container is superior with respect to insertions and deletions at the beginning and end of a sequence. The list Class container is superior with respect to insertions and deletions at any location within a sequence. Vector reallocation occurs when a member function must increase the sequence contained in the vector object beyond its current storage capacity. Other insertions and erasures may alter various storage addresses within the sequence. In all such cases, iterators or references that point at altered portions of the sequence become invalid. If no reallocation happens, only iterators and references before the insertion/deletion point remain valid.```
But back to your original problem, you need to push_back objects of type WEdge into the struct. Create an object of type WEdge and use push_back to add it to the vector.
• 12-21-2011
the data types can be integers.

the sort speed dosnt really matter, I can use Bubble sort etc like i have done in the past but would like to use the std::sort function this time

do you mean like create objects of the type WEdge (edge1....)
and then add edge1 to the vector, can the vector also be of type WEdge, like i have done below.

Code:

```         struct WEdge         {                 int From;        // an array for u points                 int To;        // an array for v points                 int Cost;        // an array for w points         };                 WEdge edge1, edge2, edge3;        // objects of the type WEdge         edge1.From = 1;         edge1.To = 2;         edge1.Cost = 5;         edge2.From = 1;         edge2.To = 2;         edge2.Cost = 5;         edge3.From = 1;         edge3.To = 2;         edge3.Cost = 5;         vector<WEdge>myEdges;         myEdges.push_back(edge1); // add edge to vector ...```
thanks.
• 12-21-2011
VirtualAce
Did it compile? If it did then I think your question has been answered. Your original question:

Quote:

but when i do it this way i cant seem to add data using push_back method.
It appears that you can now add items to the vector. As for sorting you will be sorting WEdge objects based on their weight or cost member.
• 12-21-2011
thanks it does compile.

I cant seem to print out data from the vector now, e.g. i have tried

Code:

```for(int i=0; i<myEdges.size(); i++) {         cout << "data" << i << " Is: " << myEdges[i] << "\n"; }```
how would i access edge1 data such as the To, From, Weight that is held in the vector myEdges?

thanks
• 12-21-2011
Hi, dont worry about the last post i figured out how to do it.

i am trying

std::sort(myEdges.begin.wCost, myEdges.end.wCost());

but it dosnt work and dosnt seem to like the . between myEdges and begin
• 12-21-2011
Elysia
Since your vector is a vector of a struct, the compiler has no idea on how to sort it.
You need to implement an bool operator < (const WEdge& lhs, const WEdge& rhs) that returns true when lhs is less than rhs.
You probably want to do something like

Code:

`bool operator < (const WEdge& lhs, const WEdge& rhs) { return lhs.weight < rhs.weight; }`
Then it should work.
• 12-21-2011
Hi, i have never done anything like that before, could you explain further, or like explain what the parts are doing

thanks
• 12-21-2011
Elysia
This should cover it, I think.
• 12-21-2011
VirtualAce
Quote:

As for sorting you will be sorting WEdge objects based on their weight or cost member.
This is what Elysia is referring to. You will need to overload an operator to perform the desired comparison.
• 12-21-2011
Hi, yes i understand that, I looked at the link given by Elysia but it seems very complex.

I dont understand what this code is doing, is it saying that if something on the left hand side is less than something on the right hand side, then do...

Code:

`bool operator < (const WEdge& lhs, const WEdge& rhs) { return lhs.wCost < rhs.wCost; }`
how would i link this in with the sort, and how would the sort be written because when i am writing it like below i get lots of errors, but something similar was working before.

Code:

`std::sort(myEdges.begin(), myEdges.end());`
thanks
• 12-21-2011
Elysia
Quote:

Hi, yes i understand that, I looked at the link given by Elysia but it seems very complex.

What is complex? What is it you do not understand? Pick an example and we'll explain.

Quote:

I dont understand what this code is doing, is it saying that if something on the left hand side is less than something on the right hand side, then do...
Inside sort, we need to know if an object is "less" than some other. This is how we sort. The least items go first.
In numbers, that means 0 comes before 10 because 0 < 10.
But you have a struct. So how we do know if some Wedge1 is less than some Wedge2? This is something that, we, the programmers, must define. What we must tell the compiler.

If you have two WEdge objects wedge1 and wedge2 and do wedge1 < wedge2, the compiler will look for a function called "operator <". This function must return a bool and takes two arguments to WEdge objects. The two parameters are the object on the left and to the right of the "<", respectively.
Since we want to sort by weight, what we do is we do a comparison, is the weight of the object on the left of "<" less than the one on the right?
• 12-21-2011
```        bool compareByCost (const WEdge &lhs, const WEdge &rhs)         {         return lhs.wCost < rhs.wCost;         }         std::sort(myEdges.begin(), myEdges.end(), compareByCost);```