2016-12-02 64 views
1

我有以下地圖:顯示一個字符串,矢量<int>地圖在C++

std::map<std::string, std::vector<int> > my_map; 

我這樣插入鍵和值在我的地圖:

my_map.insert(std::pair<std::string, std::vector<int> >(nom,vect)); 

如何打印兩鍵和我的地圖的值?

我已經測試:

for(std::map<std::string, std::vector<int> >::iterator ii=my_map.begin(); ii!=my_map.end(); ++ii) 
    { 
     std::cout << (*ii).first << ": " << (*ii).second << std::endl; 
    } 

但出於某種原因,我得到這個錯誤:

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘std::vector<int>’) 
std::cout << (*ii).first << ": " << (*ii).second << std::endl; 
+0

你怎麼能打印'std :: vector '?你不能這樣做,除非你通過爲'operator <<'提供一個過載來定義它是如何被打印的。'' – user463035818

+0

你需要遍歷'std :: vector ',或者在原地進行,或者通過重載'運營商<<'爲這種類型(我不建議。) – Tobias

+0

該死的我是一個初學者,我真的不需要每個評論員在這個論壇downvote一篇文章,當他們找到答案很明顯 –

回答

3

首先,使用typedef(或更現代的方法using):

typedef std::vector<int> ivector; 
typedef std::map<std::string,ivector> sivmap; 

sivmap my_map; 

// now benefits of using synonyms 
my_map.insert(sivmap::value_type(nom,vect)); 

// or even easier 
my_map.insert(std::make_pair(nom,vect)); 

// for loop is less verbose as well 
// when there is no intention to modify data use const_iterator 
for(sivmap::const_iterator ii=my_map.begin(); ii!=my_map.end(); ++ii) 
{ 
    std::cout << ii->first << ": " << ii->second << std::endl; 
} 

現在你的循環工作創造運營商

std::ostream &<<(std::ostream &out, const ivector &iv) 
{ 
    out << '['; 
    for(ivector::const_iterator it = iv.begin(); it != iv.end(); ++it) { 
     if(it != iv.begin()) out << ", "; 
     out << *it; 
    } 
    return out << ']'; 
} 

這不僅使你的代碼更短,更簡潔,但不容易出錯,更易於閱讀和修改

+0

我不是一個定義'using' typedef的輸出運算符的忠實粉絲。當它只是一個別名時,它會發送一個將其定義爲單獨類型的錯誤消息。與另一個別名不同的輸出例程會與此衝突。 –

+0

@TheVee這僅僅是一個例子,它可以很容易地更改爲模板,它可以與任何矢量協同工作,我只是不想一次性將信息匹配到OP。 – Slava

2

嗯,這是因爲沒有operator<<std::vector<int>,使得這一個這種罕見的C++編譯錯誤很容易理解和簡潔。就像您使用迭代器爲std::map編寫自己的輸出一樣,您需要爲std::vector執行類似的操作。

原因之一,該標準沒有對我們做,這是無邊的各種格式,人們可以期待的輸出出現在例如,如果你想通過一個()包圍的數據和每兩個元素用,和一個空格隔開,則更容易要求您作爲用戶在幾行內實現此操作,而不是製作允許該算法的算法,但也可以將每個元素打印在單獨的行上,並使用小數點進行對齊,並使用縮進向量的矢量的遞歸打印,其他人可能需要。

出於同樣的原因,建議您在需要它的地方格式化矢量,而不是實際執行它,並且爲它設置operator<<。如果您要在程序中的多個點使用相同的打印機制,並且希望能夠很好地書寫它,如std::cout << vector << '\n',最好的方法是創建一個公開擴展std::vector<int>的類,如

class printable_vector : public std::vector<int> { 

    using std::vector<int>::vector; // inherit constructors 
    // (all other public member functions are inherited automatically) 

    friend std::ostream& operator<< (std::ostream& os, const printable_vector& vector) { 
    // do the actual printing to os... 
    return os; 
    } 
}; 

這樣,你可以以任何方式操作printable_vector你可以正常std::vector<int>但它也提供了輸出功能。

1

您需要添加另一個用於bucle來解決這個問題:

#include <map> 
#include <vector> 
#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
    std::map<std::string, std::vector<int> > my_map; 

    string nom = "carlitos"; 
    vector<int> vect; 

    vect.push_back(1); 
    vect.push_back(2); 
    vect.push_back(3); 
    vect.push_back(4); 


    my_map.insert(std::pair<std::string, std::vector<int> >(nom,vect)); 

    for(std::map<std::string, std::vector<int> >::iterator ii=my_map.begin(); ii!=my_map.end(); ++ii) 
    { 
    for(std::vector<int>::iterator iii=(*ii).second.begin(); iii!=(*ii).second.end(); ++iii) 
    { 
     std::cout << (*ii).first << ": " << *iii << std::endl; 
    } 
    } 
} 

Test!

(這是因爲std::cout不會直接打印std::vector,通過運營商<<

相關問題