How to read vectors

How do I take a vector such as:

< (2,c) (4,T) (3,R) >
and print out a dynamic array such as this:

0---C
1---C
2---T
3---T
4---T
5---T
6---R
7---R
8---R

Just point me in the right direction, its homework. Thanks.
Last edited on
just basically wondering how to start it.
bump
Can you show the code that creates that vector?
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstdlib>
#include <vector>
#include <utility>
using namespace std ;

// --------------------------------------------------------------------------

void generateRandomPairs ( vector< pair<unsigned int,char> > & pairVector )
{
    unsigned int randSeedValue ;
    cout << "\nEnter random number generator seed value: " ;
    cin >> randSeedValue ;
    srand ( randSeedValue ) ;
    unsigned int numberOfPairs = (rand() % 4 + 3) ;
    pairVector.clear() ;
    for ( unsigned int i = 1 ; i <= numberOfPairs ; i ++ )
    {
        unsigned int randomCount = (rand() % 4 + 1) ;
        char randomUppercase = (rand() % 26 + 'A') ;
        pair<unsigned int,char> randomPair ( randomCount, randomUppercase ) ;
        pairVector.push_back ( randomPair ) ;
    } // end for loop
} // end generateRandomPairs function

// --------------------------------------------------------------------------

void printRandomPairs ( const vector< pair<unsigned int,char> > & pairVector )
{
    cout << "\nThe vector of ordered pairs is ...\n\n\t< " ;
    for ( unsigned int i = 0 ; i < pairVector.size() ; i ++ )
    {
        pair<unsigned int,char> numberLetterPair = pairVector.at(i) ;
        cout << '(' << numberLetterPair.first << ',' << numberLetterPair.second << ") " ;
    } // end for loop
    cout << ">\n\n" ;
} // end printRandomPairs function

// --------------------------------------------------------------------------

int main ()
{
    vector< pair<unsigned int,char> > pairVector ; // initially empty
    generateRandomPairs ( pairVector ) ;
    printRandomPairs ( pairVector ) ;
    
    
    
    
    
    
    cout << "The dynamic array contains ...:\n";
    cout << " " << endl;
    cout << "Index       Value";
    cout << "-----       -----";
    cout << 
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
    // HERE, STUDENT IS TO INSERT C++ CODE THAT ...                                    //
    //   1. CREATES A DYNAMIC ARRAY TO STORE LETTERS AS DESCRIBED IN PDF FILE          //
    //      (DYNAMIC ARRAY MUST BE LARGE ENOUGH TO STORE ALL LETTERS, BUT NO LARGER)   //
    //   2. POPULATE THE DYNAMIC ARRAY WITH LETTERS AS DESCRIBED IN PDF FILE           //
    //   3. PRINTS THE CONTENTS OF THE ARRAY AS ILLUSTRATED IN PDF FILE                //
    //   4. DEALLOCATES THE DYNAMIC ARRAY THAT WAS CREATED IN STEP 1                   //
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
    
} // end main function

// --------------------------------------------------------------------------

do you have to parse < (2,c) (4,T) (3,R) > and then generate a vector.
maybe i don't understand the problem exactly but this is how i did it.

http://cpp.sh/8hie
Last edited on
Basically just what you have but the array should be dynamic. You shouldn't have to input the contents like you did.
try this.

http://cpp.sh/4emo
Topic archived. No new replies allowed.