Thread: Referring to an unknown element

  1. #1
    Registered User Daniel Primed's Avatar
    Join Date
    Jun 2005
    Posts
    13

    Referring to an unknown element

    I posted about this in another help thread a week or so ago but it was only a quick side note. Now its really starting to bother me. I am creating a program in which the user any amount of names and school results in. I’m pretty much done. Except that I am unsure about how to refer the names. Since I can’t say to begin with how many names there are going to be so I can’t print x amount of elements.

    To summarise

    -I’m using vectors

    -user defines no of elements in the vectors

    -how do I print the elements(refer to them individually, so that its flexible enough)

    I’m not sure if I can just use the size and say print the element which is the size-(size-1) to refer to the first element and just increase the size of 1 as I go. Then once I get to a blank element just print some kinda of end message. If I could do something like that how would I do it.

    Thank you for the help.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You say you are using vector. So there is no need to specify any size of the vector.
    Just insert new elements wit push_back() ( this will automatically adjust the vector's size ), then there won't be any "empty" elements in the vector ( this just happens if you create your vector with a certain size or call resize on it ) You can get the actual size of the vector by calling size() on the vector.
    To access idividual elements you can use operator [].
    Kurt

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    So, your question is about printing all the elements of a vector? As an example:
    Code:
    #include <string>
    #include <iterator>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    struct person
    {
        string name;
        int age;
        person(string n,int a) : name(n), age(a) {}
    };
    
    // Overloaded stream insertion operator
    ostream& operator<<(ostream& os,const person& p)
    {
        return os << p.name << " is " << p.age << " years old.";
    }
    
    int main()
    {
        vector<person> people;
        vector<person>::size_type loop;
        vector<person>::iterator it;
    
        // Insert person objects onto vector.
        people.push_back(person("Homer",42));
        people.push_back(person("Lisa",10));
        people.push_back(person("Bart",12));
    
        // Print method #1 use manual loop and manual printing w/ indexing
        for( loop = 0; loop < people.size(); ++loop )
        {
            cout << people[loop].name << " is "
                 << people[loop].age << " years old."
                 << endl;
        }
    
        // Print method #2 use manual loop and manual printing but w/ iterators
        for( it = people.begin(); it != people.end(); ++it )
        {
            cout << it->name << " is "
                 << it->age << " years old."
                 << endl;
        }
    
        // Print method #3 use manual loop and indexing but with operator<<
        for( loop = 0; loop < people.size(); ++loop )
        {
            cout << people[loop] << endl;
        }
    
        // Print method #4 use copy STL function... (uses operator<<)
        copy(people.begin(),people.end(),ostream_iterator<person>(cout,"\n"));
        cout.flush();
    
        return 0;
    }
    All of those methods should output the same thing, so you should have 4 copies of the following:
    Code:
    Homer is 42 years old.
    Lisa is 10 years old.
    Bart is 12 years old.
    There are other ways as well, you could use a manual loop with iterators that uses the overloaded operator<< for example.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM