c++ - 返回多重映射的内容

标签 c++ c vector multimap

这里我使用了多重映射并在底部打印了它的内容。

通常我只使用数组并轻松返回和获取数组内容。

喜欢:

void main(){
char *ch;
ch=client(); //function call 
//Now we can get ch[0]...
}

char function client()
{
char ar[2]
....
return ar;
}

我可以以类似的方式处理多重 map 吗?因为我想同时返回 string 和 int 值。并使用套接字编程,因此它将使用 send 和 receive() 方法来发送和接收。

std::multimap<int,std::string>::iterator it = dst.begin();
for(int count = 0;count<3 && it !=dst.end();++it,++count)
   std::cout<<it->second<<":"<<it->first<<std::endl;

在此代码中,我想发送it->secondit->first。 什么是正确的方法?

最佳答案

是的,这会起作用,但更喜欢使用 const ierator。正如 const 所建议的(至少在最近的编译器和库中)也是线程安全的。所以更喜欢:

std::multimap<int,std::string>::const_iterator it = dst.cbegin();
for(int count = 0;count<3 && it !=dst.cend();++it,++count)
   std::cout<<it->second<<":"<<it->first<<std::endl;

关于c++ - 返回多重映射的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18557265/

相关文章:

c++ - Qt - 如何为 QProgressBar 组合 QtConcurrent 和 QThreadPool?

c++ - 引用和绑定(bind)对象

c++ - 倒序打印 vector

matlab - 在Matlab中的for循环中使用动态向量作为索引值

c++ - 如何从 std::vector 中自动删除已完成的 future

C++字符串库错误: string subscript out of range

c++ - [ '(' token 之前的预期标识符或 '{' 错误]

c - 获取C中文件的完整路径

c++ - 测试位以创建字符串 - 有更好的方法吗?

c - 'int *' differs in levels of indirection from ' int'