简体   繁体   中英

c++ loop std::vector<std::map<std::string, std::string> >

how to loop this?

i already tried :

//----- code
std::vector<std::map<std::string, std::string> >::iterator it;
for ( it = users.begin(); it != users.end(); it++ ) {
    std::cout << *it << std::endl; // this is the only part i changed according to the codes below
}
//----- error
error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::map<std::basic_string<char>, std::basic_string<char> >]’

//----- code
std::cout << *it["username"] << std::endl;
//----- error
note:   template argument deduction/substitution failed:
note:   ‘std::map<std::basic_string<char>, std::basic_string<char> >’ is not derived from ‘const std::complex<_Tp>’
//----- code
std::cout << *it->second << std::endl; // also tried with parenthesis - second()
//----- error
error: ‘class std::map<std::basic_string<char>, std::basic_string<char> >’ has no member named ‘second’
//----- code
for( const auto& curr : it ) std::cout <<  curr.first() << " = " << curr.second() << std::endl;
//----- error
error: unable to deduce ‘const auto&’ from ‘<expression error>’

and lastly

//----- code
std::map<std::string, std::string>::iterator curr, end;
for(curr = it.begin(), end = it.end();  curr != end;  ++curr) {
    std::cout <<  curr->first << " = " << curr->second << std::endl;
}
//----- error
‘std::vector<std::map<std::basic_string<char>, std::basic_string<char> > >::iterator’ has no member named ‘begin‘ & ‘end’

i hope i give a clear detail.. the above is code then below is the error.. and currently my mind is blank.

and sorry about this..

i already make it work on this type : std::map<int, std::map<std::string, std::string> > and im trying to use vector as an option.

Your code for iterating is correct; the problem is your output statement. Your code is doing this:

std::cout << *it << std::endl;

In this case, *it refers to a std::map<string,string> and std::cout doesn't know how to output a map. Perhaps you want something like this:

std::cout << (*it)["username"] << std::endl;

Make sure to use ()s around *it otherwise you'll have operator precedence issues.

std::vector<std::map<std::string, std::string> >::iterator it;
for ( it = users.begin(); it != users.end(); it++ ) {
    std::cout << *it << std::endl;

When users isn't .empty() , the << operator above tries to stream a std::map<std::string, std::string> object, but the Standard Library doesn't provide an overload to stream maps: how would it know what separators you wanted between keys and values, and between elements?

I suggest you break the problem down like this:

std::vector<std::map<std::string, std::string> >::iterator it;
for ( it = users.begin(); it != users.end(); it++ )
{
    std::map<std::string, std::string>& m = *it;

    for (std::map<std::string, std::string>::iterator mit = m.begin();
         mit != m.end(); ++mit)
        std::cout << mit->first << '=' << mit->second << '\n';

    std::cout << "again, username is " << m["username"] << '\n';
}

This can be simplified in C++11:

for (auto& m : users)
    for (auto& kv : m)
        std::cout << kv.first << '=' << kv.second << '\n';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM