Vector Struct

This is a discussion on Vector Struct within the C++ Programming forums, part of the General Programming Boards category; Hi, i am trying to create a vector a structure type my code so far is Code: struct WEdge { ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    55

    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

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

    Explain the task at hand and perhaps we can help you create a better data structure.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    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

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Ok so what are the data types you are going to use for 'from' 'to' and 'weight'?

    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.
    Last edited by VirtualAce; 12-21-2011 at 12:39 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    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.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Did it compile? If it did then I think your question has been answered. Your original question:

    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.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    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

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, dont worry about the last post i figured out how to do it.

    i am confused about sorting

    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

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, i have never done anything like that before, could you explain further, or like explain what the parts are doing

    thanks

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    This should cover it, I think.
    Operator Overloading in C++ - Cprogramming.com
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    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

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by Khadafi View Post
    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.

    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi thanks for all your help so far, i am trying

    Code:
    	bool compareByCost (const WEdge &lhs, const WEdge &rhs)
    	{
    	return lhs.wCost < rhs.wCost;
    	}
    
    
    	std::sort(myEdges.begin(), myEdges.end(), compareByCost);
    what am i doing wrong?

    thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of a struct containing another vector
    By Todd88 in forum C++ Programming
    Replies: 61
    Last Post: 12-05-2008, 05:38 PM
  2. Class/Struct in a Vector...
    By yaya in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2008, 03:16 AM
  3. Using a vector of pointers in a struct...HELP!
    By misterAction in forum C++ Programming
    Replies: 4
    Last Post: 12-01-2006, 10:43 AM
  4. struct + vector = fun
    By The Brain in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 06:29 AM
  5. vector<struct>
    By Luigi in forum C++ Programming
    Replies: 6
    Last Post: 01-02-2003, 11:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21