如何打印带矢量的多图元素?

时间:2013-05-14 09:47:59

标签: c++

我创建了multimap

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

我也使用以下方法将元素插入其中:

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

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

2 个答案:

答案 0 :(得分:2)

您可以为operator<<vector覆盖multimap。打印代码看起来会更容易:

#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

如果您不想覆盖运营商&lt;&lt; (我想知道为什么......)你可以做到这一点:

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

答案 1 :(得分: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());