Solved

simple Vector STL question

Posted on 2003-10-28
5
435 Views
Last Modified: 2013-12-14
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
Comment
Question by:jjacksn
5 Comments
 
LVL 48

Expert Comment

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

Author Comment

by:jjacksn
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

by:
AlexFM earned 100 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

by:Axter
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

by:fsign21
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 {
    cout << "Not Found" << endl;
  }
  string* p1 = vec.findName("Tom");
  if(p1) {
    cout << "Found" << endl;
  } else {
    cout << "Not Found" << endl;
  }
  return (0);
}

Output:
Found
Not Found
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now