Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,519 people online right now. Registration is fast and FREE... Join Now!




vector of strings

 
Reply to this topicStart new topic

vector of strings

jayhuang
3 Dec, 2006 - 08:32 PM
Post #1

New D.I.C Head
*

Joined: 11 Oct, 2006
Posts: 31


My Contributions
given a vector of strings, write code which outputs the 3rd and 5th character of each string.

Here is what I have.

CODE

#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
  vector <string> hold;

  hold.push_back("speech");
  hold.push_back("wireless");
  hold.push_back("professor");

  string temp;

  for (int i = 0; i < hold.size(); i++)
   {
    temp =  hold[i];
    cout << "3rd is " << temp[2] << endl;
    cout << "5th is " << temp[4] << endl;
   }
   return 0;
}


This post has been edited by jayman9: 3 Dec, 2006 - 10:17 PM
User is offlineProfile CardPM
+Quote Post

Xing
RE: Vector Of Strings
3 Dec, 2006 - 09:00 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
1. You don't need temp
CODE

#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
vector <string> hold;

hold.push_back("speech");
hold.push_back("wireless");
hold.push_back("professor");

for (unsigned int i = 0; i < hold.size(); i++)
{
cout << "3rd is " << hold[i][2] << endl;
cout << "5th is " << hold[i][4] << endl;
}
return 0;
}


2. Changed (int i = 0; i < hold.size(); i++) to (unsigned int i = 0; i < hold.size(); i++) to remove the warning between unsigned and signed comparison.

3. Closing code tag is [/code]

This post has been edited by Xing: 3 Dec, 2006 - 09:01 PM
User is offlineProfile CardPM
+Quote Post

okyup
RE: Vector Of Strings
3 Dec, 2006 - 09:04 PM
Post #3

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
What is your question? The code you posted works... unsure.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:01PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month