vector<struct>

This is a discussion on vector<struct> within the C++ Programming forums, part of the General Programming Boards category; I have the following structure : struct date { string name; int day; int month; int year; }; then i ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    vector<struct>

    I have the following structure :

    struct date
    {
    string name;
    int day;
    int month;
    int year;
    };

    then i put it into a vector :

    vector<date> cal;

    how do I acces cal members?
    how do can I copy a variable into name or day?

    luigi
    thx

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    One way is to use a temp struct to gather data;
    Code:
    struct date
    {
    string name;
    int day;
    int month;
    int year;
    };
    
    
    int main()
    {
       vector<date> cal;
    
       date temp;
    
       temp.name = "Jan";
       temp.date = 2;
       temp.month = 1;
       temp.year = 2003;
      
       cal.push_back(temp);
      
        //etc.

    another way is declare vector with a size

    vector <date> cal(12);

    then I think you can do this:

    cal[0].name = "Jan";
    cal[1].month = 10;

    or whatever.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here are some ways to do it. Probably more but I didn't feel like writing the end all be all STL guide This should be sufficient for what you want to do though. Enjoy

    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    using namespace std;
    
    struct Data
    {
      string name;
      int number;
    };
    
    int main( int argc, char **argv )
    {
      // Create an instance of the Data struct
      Data SampleData;
    
      // Create a vector of Data structs
      vector< Data > vData;
      
      // Push the SampleData instance onto the vector
      vData.push_back( SampleData );
    
      // Multiple ways to access the Data structure..
    
      // Method #1 , access just like an array
      vData[0].number = 123;
      vData[0].name = "John Doe";
    
      // Method #2 , using front and back accessors
      // In this case, front and back are the same since we only
      //   have one instance in our vector..
      vData.front().name = "John Doe";
      vData.front().number = 555;
    
      vData.back().name = "John Doe";
      vData.back().number = 321;
    
      // Method #3 , using an iterator
      // Use an iterator to access the contents of the vector
      vector< Data >::iterator iter;
    
      // Set the iterator to first element
      iter = vData.begin();
    
      iter->name = "John Doe";
      iter->number = 456;
    
      return 0;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the basic problem as I see it is you don't know how much, if any, memory has been allotted to cal when you do this:

    vector <date> cal;

    Maybe in your implementation of STL the initial capacity of a vector is memory enough for 10 elements and in mine the initial capacity is 0. What I do know is that if you try writing to an area of memory that you don't have valid access to you can cause problems. Therefore, use the push_back method, which will always add to the end of the vector, assuming you haven't ran out of memory (the vector will self expand if the addition of the current date causes the current size to exceed the current capacity) --OR-- use a vector with a known size, in which case I think you can randomly assign values, but then again, can't say as I've tried to do that but it's what happens in hashing, I think, so maybe it works here too (WOW--reminder to self---slow down on the caffeine). I do know that once you assign values to individual elements in cal you can randomly access them, and then manipulate/change them, but I don't know if you can randomly assign them or not.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    using [] is inherently evil (??) thats whati was taught. on some platforms/compilers you get a .at() function that you can use. idk really, havent used vectors for a while.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by Flikm
    using [] is inherently evil (??) thats whati was taught. on some platforms/compilers you get a .at() function that you can use. idk really, havent used vectors for a while.
    [] doesn't check the bounds. It is therefore faster, but more dangerous. If you know that your value is in the bounds of the vector, than you should be fine.

    .at() on the other hand checks bounds and is therefore safer.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    oh, segfaults :P. [] IS evil!!!

Popular pages Recent additions subscribe to a feed

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