1 Replies - 198 Views - Last Post: 23 April 2011 - 04:04 PM Rate Topic: -----

#1 hannahyancy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 25-March 11

Emptying a vector and printing the string equivalent

Posted 23 April 2011 - 04:01 PM

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;
}



I know that my code compiles but its not printing out the correct vector, and im not sure what is wrong with it. If you know how to fix it so that it will print out the correct string vector PLEASE try and help me and explain to me what exactly to do and how to do it, because I am super lost.
Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Emptying a vector and printing the string equivalent

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Emptying a vector and printing the string equivalent

Posted 23 April 2011 - 04:04 PM

Duplicate threads closed. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1