C++ 如何打印具有向量的多重贴图元素?
我已经创建了C++ 如何打印具有向量的多重贴图元素?,c++,C++,我已经创建了多重映射 multimap-mt 此外,我还使用以下方法将元素插入其中: mt.insert(std::make_pair(threadid,funcname)) 如何使用键和值对在多重映射中打印元素?您可以使用以下方法 #include <map> #include <vector> #include <string> #include <iostream> using namespace std; int main() { mul
多重映射
multimap-mt代码>
此外,我还使用以下方法将元素插入其中:
mt.insert(std::make_pair(threadid,funcname))代码>
如何使用键和值对在多重映射中打印元素?您可以使用以下方法
#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;
}
}
编辑:
用下面的行替换cout行
printf("%s\n", (it->second)[i].c_str());
你可以用下面的方法
#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;
}
}
编辑:
用下面的行替换cout行
printf("%s\n", (it->second)[i].c_str());
您可以覆盖codevector/code和codemultimap/code的运算符
({“foutry”,“two”}));
用于(常量自动和i:m){
std::cout您可以覆盖运算符
({“foutry”,“two”}));
用于(常量自动和i:m){
std::cout您的问题是什么?迭代多重映射
?打印/格式化向量
?可能会给您一些提示。您的问题可以从几个方面进行改进。您可以提供一个预期输出格式的示例,并描述您已经尝试过但不起作用的内容。funcname
是s的向量吗trings?我的问题是在multimap中打印向量?我尝试使用for(multimap::iterator t=mt.begin();t!=mt.end();++t){coutw你的问题是什么?迭代多重映射
?打印/格式化向量
?可能会给你一些提示。你的问题可以从几个方面改进。你可以提供一个预期输出格式的示例,并描述你已经尝试过但不起作用的内容。funcname
是strin的向量吗gs?我的问题是在多重映射中打印向量?我尝试使用for(多重映射::迭代器t=mt.begin();t!=mt.end();++t){coutThanks more for reply.for循环中的最后一个cout语句给了我一个错误,它说error:不匹配Operator已经包含iostream头文件bt仍然面临这个问题这对我来说很奇怪不起作用。有没有其他方法来打印ector值,因为这对mecout不起作用,必须对std::string起作用。如果是,我s不起作用,我们可以使用printf。更新了我的答案。实际上向量不是使用c_str函数的字符串……实际的多重映射如下……多重映射mt;非常感谢您的回复。for循环中的最后一个cout语句为我提供了错误,错误:与operator不匹配已包含iostream头文件仍然面临这个问题这对我来说很奇怪不起作用。有没有其他方法来打印向量值,因为这对mecout不起作用必须对std::string起作用。如果不起作用,我们可以使用printf。更新了我的答案。实际上向量不是字符串来使用c_str函数……实际的多重映射如下……multimap mt;除了覆盖运算符,还有其他方法吗?是的,是的。从运算符中取出代码我不明白你在说什么。你能说清楚吗?我编辑了一个答案。如果你不能使用C++11,我可以再做一次。)它给出了错误。在“:”行标记之前,这里不允许使用函数定义对于(const auto&i:m){除了覆盖操作符之外,还有其他方法吗?是的,是的
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;
}
}
}
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;
}
}
}