Pik93
Pik93

Reputation: 49

C++ Iterator List

I have one doubt. I have one object list with some data (in this case to store names).

I iterate the list: (pNames it is my list receive by parameter)

std::string names;
std::list<Names>::const_iterator it = pNames->begin();
while(it != pNames->end())
{
   std::cout << names << it->namesUser;
   ++it;
}

The problem: I need to save the all values of iterator in std::string to use this string on mysql query (for example select age from users where names in ('names');) In this momment with the `std::cout << names << it->namesUser; i can see all names in list but i cannot use the names variable in query for example. It only work with cout. I'm new with c++ programming, so what is the way to store all iterator values in string?

Thanks a lot.

Regards

Upvotes: 0

Views: 174

Answers (1)

Edd
Edd

Reputation: 1370

Use std::ostringstream instead of std::cout

http://www.cplusplus.com/reference/sstream/ostringstream/

#include <sstream>

....

std::ostringstream names;
std::string delimiter = ",";
std::list<Names>::const_iterator it = pNames->begin();
if (it != pNames->cend())
{
    names << it->pNames;
    it++;
}
while(it != pNames->cend())
{
    names << delimiter << it->namesUser;
    ++it;
}
std::string result = names.str();

Upvotes: 1

Related Questions