?
Solved

STL

Posted on 2005-05-07
3
Medium Priority
?
304 Views
Last Modified: 2013-12-14
I need to store a list of strings and to be able to access n-th element of this list. In which convenient way would you suggest to implement it (what data type and example how to get n-th element)
0
Comment
Question by:Buffon
  • 2
3 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13950760
definitively a vector<>, since list<> is not efficient for this task
0
 
LVL 11

Author Comment

by:Buffon
ID: 13950763
can you supply any code examples, please?
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 13950795
#include <iostream>
#include <string>
#include <vector>
using namespace std;

........

vector<string> myStrings;

string a = "some string";
string b = "another string";
string c = "last string";

myStrings.push_back(a);
myStrings.push_back(b);
myStrings.push_back(c);

cout << "String 1: " << myStrings[0] << endl;   // Notice zero-based array index
cout << "String 2: " << myStrings[1] << endl;
cout << "String 3: " << myStrings[2] << endl;
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question