Vector question

Writing a program that takes a set of 5 scores displays them in order then when a new higher number is put in replaces the past low number. I want to be able to add initials in correlation with the scores. Can I create a vector that will store both integers and strings, and then sort just the integers?

Or how do I get the string vector to follow the same order as my sorted integer vector?

Thanks for any input.
Can I create a vector that will store both integers and strings, and then sort just the integers?

Yes, if you use the std::pair class for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <utility>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
     typedef pair<int, string> intStringPair;

     intStringPair aPair(42, "hello");

     cout << aPair.first << endl; //prints 42
     cout << aPair.second << endl; //prints hello

     vector<intStringPair> vectorContainingIntsAndStrings;

     vectorContainingIntsAndStrings.push_back( intStringPair( 25, "H.B.") );
     vectorContainingIntsAndStrings.push_back( intStringPair( 39, "L.K.") );
     vectorContainingIntsAndStrings.push_back( intStringPair( 56, "J.L.") );

     //you can sort based on vectorContainingIntsAndStrings[x].first

     return 0;
}
Topic archived. No new replies allowed.