0

I have a command line C++ program that lets you enter basic information about a person (ID number, name, age, etc.) and I want to output to a console in the following manner:

-------------------------------------------------------------------
Index   ID #        First Name          Last Name           Age
-------------------------------------------------------------------
0       1234        John                Smith               25   

The person objects are stored in an array of Persons and I've overload the ostream (<<) operator to print out all of the fields like you see. The dashed lines and header come from a displayHdg() function. Anyhow, I have not been able to figure out how to get the proper index value for the array. Ideally, I'd like to generate the indices for each line, but all my attempts have failed. The array is looped through and each object printed in the main() function, and the ostream is overloaded in a person class, so I tried to use global variables as well as static variables, and all of those produce incorrect numbering (i.e. show 0, 1 the first time (for 2 objects), then change to 1, 2 on the next display). Any ideas?

John Smith
  • 9
  • 1
  • 3

4 Answers4

1

Wouldn't this work? (formatting of ID field ommitted)

vector<Person> v;

for (int i = 0; i < v.size(); ++i)
  cout << i + 1 << v[i] << endl;

This starts indexing at 1.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • For some reason, the most obvious answer eluded me until I re-read your code, Eli. It was way too simple...All I had to do was go to the for loop where I printed out each line and have it print out i. For some reason, that never even occurred to me as I was trying to associate the index with each Person object. Thank you very much, and I will try not to make this mistake again. – John Smith Mar 11 '10 at 04:52
0

EDIT:

OK now I see what you want. You want to find an element in the vector!

std::vector<person>::iterator p = 
          std::find(Persons.begin(), Persons.end(), element);

if( p != Persons.end() )
{
  std::cout << "index of element is: " << p-Persons.begin();
}

If you have the correct formating, you should be able to do the following:

for(size_t i = 0; i < Persons.size(); ++i)
{
  cout << i << '\t' << Persons[i] << endl;
}

I would recommend taking a look at the formatting facilities in brief in this post. Using setw, left, right... manipulators is better than doing it manually.

Community
  • 1
  • 1
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
  • Right, that's how I have it right now. I've used setw, left, right, setfill, etc. My problem is trying to print the index value of an array element. Normally in say Java or C#, I'd do Array.indexOf(element) and that would give me the element's index. Is there an equivalent in C++? – John Smith Mar 11 '10 at 04:43
  • What I needed was even simpler (see my own answer), but thank you very much for the find in vector function. I was wondering how to do that as well. Would I be able to use the find function with arrays too? – John Smith Mar 11 '10 at 04:55
  • @John: yes, that's the power of the STL container/algorithm separation. You just have to pass iterators representing the beginning and end of the content, and pointers are usable as iterators. So find(array, array+length, predicate) or maybe those arguments in a different order. – Ben Voigt Mar 11 '10 at 05:11
0

You need to use "find" algorithms to find exact index of Person object in vector < Person>.

Sad Developer
  • 1,258
  • 1
  • 12
  • 16
0

You could use wrapper class to hold index and print it according to your formatting in operator<<:

// wrapper to hold index
template<typename T>
struct Ti
{
    Ti( size_t index, const T& t ) : index(index), val(t) {}
    size_t index;
    const T& val;
};

// you class
struct X
{
    friend ostream& operator<<( ostream& out, Ti<X>& t );
protected:
    int some_data;
};

// operator<< for X
ostream& operator<<( ostream& out, Ti<X>& t )
{
    out << "test " << t.index << "  " << t.val.some_data;
    return out;     
}

int main()
{
    vector<X> xxx;
    for ( size_t i =0; i < xxx.size(); ++i)
        cout << Ti<X>(i+1, xxx[i]) << endl;
}
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212