0

我创建了multimap.

multimap<int, std::vector<string> > mt;.

我还使用以下方法向其中插入了元素:

mt.insert(std::make_pair(threadid, funcname));

如何使用键值对打印多图中的元素?

4

2 回答 2

2

您可以覆盖operator<<和。打印代码看起来更容易:vectormultimap

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

template< class T >
std::ostream & operator << ( std::ostream & os, const std::vector< T > & v ) {
    for ( const auto & i : v ) {
        os << i << std::endl;
    }
    return os;
}

template< class K, class V >
std::ostream & operator << ( std::ostream & os, const std::multimap< K, V > & m ) {
    for ( const auto & i : m ) {
        os << i.first << " : " << std::endl;
        os << i.second << std::endl;
    }
    return os;
}

int main() {
    std::multimap<int, std::vector< std::string > > m;

    m.insert(std::make_pair( 1, std::vector< std::string >( {"one", "two", "three" } ) ) );
    m.insert(std::make_pair( 10, std::vector< std::string >( {"ten", "twenty", "thirty" } ) ) );
    m.insert(std::make_pair( 42, std::vector< std::string >( {"foutry", "two" } ) ) );

    std::cout << m;
}

http://ideone.com/LKDPtX

如果您不想覆盖运算符 << (我想知道为什么...),您可以就地执行它:

int main() {
    std::multimap<int, std::vector< std::string > > m;

    m.insert(std::make_pair( 1, std::vector< std::string >( {"one", "two", "three" } ) ) );
    m.insert(std::make_pair( 10, std::vector< std::string >( {"ten", "twenty", "thirty" } ) ) );
    m.insert(std::make_pair( 42, std::vector< std::string >( {"foutry", "two" } ) ) );

    for ( const auto & i : m ) {
        std::cout << i.first << " : " << std::endl;
        for ( const auto & i_v : i.second ) {
            std::cout << i_v << std::endl;
        }
    }
}

最后,如果你不能使用 C++11(我也想知道为什么......)你可以像这样更改代码:

int main() {
    typedef std::vector< std::string > strings_t;
    typedef std::multimap<int, strings_t > map_t;
    map_t m;

    {
        strings_t v;
        v.push_back( "one" ); v.push_back( "two" ); v.push_back( "three" );
        m.insert( std::make_pair( 1, v ) );
    }
    {
        strings_t v;
        v.push_back( "ten" ); v.push_back( "twenty" ); v.push_back( "thirty" );
        m.insert( std::make_pair( 10, v ) );
    }
    {
        strings_t v;
        v.push_back( "foutry" ); v.push_back( "two" );
        m.insert( std::make_pair( 42, v ) );
    }

    for ( map_t::const_iterator i = m.begin(), e = m.end(); i != e; ++i ) {
        std::cout << i->first << " : " << std::endl;
        for ( strings_t::const_iterator i_v = i->second.begin(), e_v = i->second.end(); i_v != e_v; ++i_v ) {
            std::cout << (*i_v) << std::endl;
        }
    }
}

http://ideone.com/S80nLj

于 2013-05-14T10:36:03.357 回答
1

您可以使用以下方式。

#include <map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;

int main()
{
multimap<int, std::vector<string> > mt;
vector<string> v;
v.push_back("one");
v.push_back("two");

mt.insert(pair<int, vector<string> >(1, v));

multimap<int, std::vector<string> > ::iterator it = mt.find(1);

if(it != mt.end())
{
    for(size_t i = 0; i < (it->second).size(); i++)
       cout<<(it->second)[i]<<endl;
}

}

输出:

one
two

编辑:

用下面的行替换 cout 行

printf("%s\n", (it->second)[i].c_str());
于 2013-05-14T10:04:15.010 回答