Thread: Vector Struct

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What errors do you get?
    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.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Vector Struct-errors-jpg

    i have attached a screenshot of the errors i am getting

    thanks

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    compareByCost is a function, so move it outside main or whatever other function you have it placed inside.
    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.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, when i move it outside, it says that WEdge is undefined.
    do i also need a declaration of the function at the top as it is a function?

    thanks

  5. #20
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1

    A suggestion, implement a constructor for your WEdge struct:
    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(int f = 0, int t = 0, int c = 0) : From(f), To(t), Cost(c) {}
    };
    Then your vector/pushback code (post #5) can be simplified:
    Code:
    vector<WEdge>myEdges;
         
    myEdges.push_back(WEdge(1,2,5)); // add edge to vector
    myEdges.push_back(WEdge(2,6,10)); // add edge to vector
    myEdges.push_back(WEdge(3,4,7)); // add edge to vector




    #2
    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?
    You could do a couple things to access the elements. Remember myEdges is the vector of WEdge objects. Therefore, myEdges[i] is an element of this vector, i.e. a WEdge object. A WEdge object has a From/To/Cost member and so to access it you could do this:
    Code:
    for(int i=0; i<myEdges.size(); i++)
    {
    	cout << "data" << i << " Is: " << myEdges[i].From << ' ' << myEdges[i].To << ' ' << myEdges[i].Cost << "\n";
    }
    A better method however might be to look at overloading operator<< which is the stream insertion operator (writes to a stream). You could then do exactly what you had tried above since then the compiler would know how to write a WEdge object using <<. An example:
    Code:
    struct city
    {
        std::string name;
        int population;
        city(std::string n,int pop) : name(n), population(pop) {}
    };
    
    std::ostream& operator<<(std::ostream& os, const city& rhs)
    {
        return os << "City name: " << rhs.name << "\nPopulation: " << rhs.population;
    }
    
    ...
    
    city my_city("San Diego",1000000);
    
    std::cout << my_city << std::endl;
    Should output something like:
    Code:
    City name: San Diego
    Population: 1000000
    Last edited by hk_mp5kpdw; 12-21-2011 at 02:43 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Khadafi View Post
    do i also need a declaration of the function at the top as it is a function?
    Yes.
    Also, though this isn't necessary, you may find this interesting:
    C++11 - Lambda Closures, the Definitive Guide - Cprogramming.com
    It is related to your problem. In a way.
    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.

  7. #22
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    thanks hk_mp5kpdw, i have done that to simply push_back, thats great =) thanks

    Elysia...i am not sure what to do, i have put the function outside, and put a function header, but it keeps saying that WEdge is undefined, would i need to put the WEdge struct outisde aswell.
    Why do i need to say WEdge in the bool code as we are comparing things inside the vector. ahh im so confused, sorry for all the silly questions.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Show your code.
    sort is comparing elements inside the vector, but will call upon your comparison function to see which one of two elements in the lesser one.
    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.

  9. #24
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Code:
    bool compareByCost (const WEdge &lhs, const WEdge &rhs)
    	{
    	return lhs.wCost < rhs.wCost;
    	}
    Last edited by Khadafi; 12-21-2011 at 03:00 PM.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, put your struct before the compareByCost function declaration.
    Also change

    "for(int i=0; i<myEdges.size(); i++)"
    to
    "for(std::size_t i=0; i<myEdges.size(); i++)"

    And similarly for the other for loop. That will eliminate warnings (which you should pay heed to!).
    The size() function does not return an int.
    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.

  11. #26
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    thats what i was thinking but i remember someone telling me that you shouldnt make things global like that.
    but thanks it all seems to be working, i really appreciate everyones help so much

    thanks =)

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you should avoid are global variables.
    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.

  13. #28
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    oh okay.

    thanks very much.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No problem. Check back if you have further problems.
    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.

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, 06:38 PM
  2. Class/Struct in a Vector...
    By yaya in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2008, 04: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, 11:43 AM
  4. struct + vector = fun
    By The Brain in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 07:29 AM
  5. vector<struct>
    By Luigi in forum C++ Programming
    Replies: 6
    Last Post: 01-03-2003, 12:44 AM