Thread: Vector of a struct containing another vector

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    122

    Vector of a struct containing another vector

    Okay I am implementing an index of words from an input text file. I have a class that contains a vector declared like this:

    Code:
    vector<indexWord> data;
    and a struct declared like this:

    Code:
    struct indexWord {
            std::string value;
            std::vector<int> locations;
    };
    So, in essence, I am storing the words and page numbers in the indexWord struct and then storing that info in the data vector. I have an output function that should output these values. How do I go about telling the function to output the "value" and vector of locations using just the data vector?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What exactly is your question? Are you asking how to loop through every element of a vector? Do you want to have cout << indexWord_object do the right thing? What?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Yeah I need to loop through the data vector to output the "value" and vector of locations using the data vector. I don't know how to do this.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You could add a print() method to indexWord, then iterate through data calling the print() method of each element.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's why vector has iterators. You start at begin(), and you go until you get to end(), and ... well whatever you need to do, you do.
    Code:
    for (vector<indexWord>::iterator current=data.begin(); current!=data.end(); ++current) {
        // do whatever you need to do to the current object, which is *current
    }

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Code:
     invalid conversion from `const indexWord* const' to `indexWord*'|
    ||=== Build finished: 1 errors, 0 warnings ===|

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    What's the code on the line referenced by that error message?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    What tabstop just gave me:

    Code:
    for (vector<indexWord>::iterator current=data.begin(); current!=data.end(); ++current)

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Use iterators on both vectors. The first iterator will iterate through indexWord objects and the second iterator will iterate through your int vector in the objects. But this structure is also suited to a map. Key off the map on the string value and the map value will be the vector of integers.

    Code:
    typedef std::vector<int> IntVector;
    typedef IntVector::iterator IntVectorIter;
    typedef IntVector::const_iterator IntVectorConstIter;
    
    typedef std::map<std::string,IntVector> MapObject;
    typedef MapObject::iterator MapObjectIter;
    typedef MapObject::const_iterator MapObjectConstIter;
    
    
    MapObject  AllObjects;
    
    //Iterate through map
    MapObjectIter map_iter(AllObjects.begin());
    MapObjectIter map_end(AllObjects.end());
    
    while (map_iter != map_end)
    {
        IntVectorIter vector_iter(map_iter.second.begin());
        IntVectorIter vector_end(map_iter.second.end());
    
        while (vector_iter != vector_end)
        {
            //Do something with vector_iter
        }
    }
    Last edited by VirtualAce; 12-04-2008 at 10:38 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you didn't do vector<indexWord> data. What is data?

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    There's nothing wrong with that line of code. What's in the for loop?

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    data is the vector...

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by R.Stiltskin View Post
    There's nothing wrong with that line of code. What's in the for loop?
    nothing right now. just trying to see if that line of code works and it spit out that error.

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Must be a problem someplace else. This compiles fine for me:
    Code:
    #include <iostream>
    #include <vector>
    
    
    struct indexWord {
            std::string value;
            std::vector<int> locations;
    
            indexWord(std::string s) : value(s) {}
    };
    
    int main() {
      std::vector<indexWord> data;
      data.push_back(indexWord(std::string("once")));
      data.push_back(indexWord(std::string("upon")));
      for(std::vector<indexWord>::iterator current=data.begin(); current!=data.end(); ++current) {
        std::cout << current->value << " ";
      }
      std::cout << "\n";
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure it isn't a list? (I think list iterators are const pointers to const, but I don't remember for sure.) Anyway, what you posted doesn't match. So post everything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM