0

Write a program which initializes two vectors, one of type integer, the other of string. Populate the string type vector with five strings (‘one’, ‘two’, ... ‘five’). Populate the integer vector with [ 5 4 3 2 1 1 2 3 4 5 ]. Write a function that empties the integer vector while printing the string equivalent of each number in the vector. Use the integers as indexes for the string vector you created in order to accomplish this task.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(void)
{
    vector<int> intVector;

    intVector.push_back(5);
    intVector.push_back(4);
    intVector.push_back(3);
    intVector.push_back(2);
	intVector.push_back(1);
	intVector.push_back(1);
	intVector.push_back(2);
	intVector.push_back(3);
	intVector.push_back(4);
	intVector.push_back(5);

	cout << "Integers: " << endl;
    for(unsigned int i=0; i < intVector.size(); i++) 
    cout << intVector[i] << " ";
    cout << "\n" << endl;

	vector<string> strVector;

	strVector.push_back("one");
	strVector.push_back("two");
	strVector.push_back("three");
	strVector.push_back("four");
	strVector.push_back("five");

	cout << "Strings: " << endl;
	for(unsigned int i=0; i < strVector.size(); i++) 
    cout << strVector[i] << " ";
    cout << "\n" << endl;


  for(unsigned int i=0; i < intVector.size(); i++) 
  {
      intVector.erase( intVector.begin() );
	  cout << strVector[i] << " ";
  }

  cout << "\n" << endl;
  
  return 1;
}

my code wont print pass the first 5, but it ignore the 5 4 3 2 1.... please help!

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by jonsca
0

2 things to note:
There's a relationship between the number being read in and the desired index of the string vector.

When you are erasing on line 43, you are changing the vector's size, which affects your counter.

I'm sure you've had iterators in class too...

Edited by jonsca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.