Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# simple Vector STL question

Posted on 2003-10-28
Medium Priority
439 Views
vector<string> names;

for(vector<string>::iterator i = names.begin(); i !=names.end(); i++ )
{
//how do I want a pointer to the strings in names, how do i get it?
}

I need pass a string* to another function, i'm getting an error because i is an iterator, how can I recover the pointer to the string?
0
Question by:jjacksn
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 48

Expert Comment

ID: 9639651
for(vector<string>::iterator i = names.begin(); i !=names.end(); i++ )
{
cout "String: " << *i << "  Length: " << (*i).length();
}
0

LVL 5

Author Comment

ID: 9639657
I need the actual string *, I don't just want to print it out.  How do I get it back?
0

LVL 48

Accepted Solution

AlexFM earned 400 total points
ID: 9639671
*i is string itself. &(*i) is string*

for(vector<string>::iterator i = names.begin(); i !=names.end(); i++ )
{
OtherFunction( &(*i) );
}
0

LVL 30

Expert Comment

ID: 9640828
You can also point to the string const char via c_str() member function.

char data[99];
for(vector<string>::iterator i = names.begin(); i !=names.end(); ++i)
{
strcpy(data, i->c_str());
}

Here's a more optimize loop:
for(vector<string>::iterator i = names.begin(), i_end = names.end(); i != i_end; ++i)
{
strcpy(data, i->c_str());
}
0

LVL 1

Expert Comment

ID: 9641305
>I need pass a string* to another function, i'm getting an error because i is an iterator,
>how can I recover the pointer to the string?

May be, you want to do something like this?

using namespace std;
class VecStr
{
public:
vector<string> names;

string* findName(const string& val) {
for(vector<string>::iterator i = names.begin(); i !=names.end(); i++ )
{
if((*i)==val)
return &(*i);
}
return NULL;
}
};

int main (int argc, char* argv[])
{
VecStr vec;
vec.names.push_back("Bob");
vec.names.push_back("Ron");

string* p = vec.findName("Bob");
if(p) {
cout << "Found" << endl;
} else {
}
string* p1 = vec.findName("Tom");
if(p1) {
cout << "Found" << endl;
} else {
}
return (0);
}

Output:
Found
0

## Featured Post

Question has a verified solution.

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

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there isâ€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month8 days, 18 hours left to enroll