2015-02-12 4 views
0

Как это сделать?C++ loop std :: vector <std :: map <std :: string, std :: string>>

я уже пробовал:

//----- 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>’ 

и, наконец,

//----- 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’ 

я надеюсь, что я дать четкую детализацию .. выше это код, то ниже ошибка .. и в настоящее время мой ум пустым ,

и сожалеем об этом ..

я уже заставить его работать на этом типе: std::map<int, std::map<std::string, std::string> > и им пытаются использовать вектор в качестве опции.

ответ

2

Ваш код для итерации правильный; проблема заключается в вашем выводе. Ваш код делает это:

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

В этом случае *it относится к std::map<string,string> и std::cout не знает, как вывести карту. Может быть, вы хотите что-то вроде этого:

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

Убедитесь в использовании() S вокруг *it в противном случае вы будете иметь проблемы оператор старшинства.

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

Когда users не .empty(), оператор << выше пытается поток в std::map<std::string, std::string> объект, но стандартная библиотека не обеспечивает перегрузку для потоковой передачи карт: как бы это знать, что вы хотите Сепараторы между клавишами и значений и между элементами?

Я предлагаю вам разбить проблему вниз, как это:

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'; 
} 

Это может быть упрощено в C++ 11:

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

СПАСИБО БОЛЬШОЕ ... я перепробовал все ваши ответы и все работает. – xeroblast

Смежные вопросы