C++当到达地图末尾时进行地图跟踪

发布于 2024-09-06 04:20:26 字数 744 浏览 3 评论 0原文

目前我有一张地图,可以打印出以下内容

map<string, map<int,int> > mapper;
map<int,int>::iterator inner;
map<string, map<int,int> >::iterator outer;


for(outer = mapper.begin(); outer != mapper.end(); outer++){
    cout<<outer->first<<": ";
  for(inner = outer->second.begin(); inner != outer->second.end(); inner++){
      cout<<inner->first<<","<<inner->second<<",";
  }
}

截至目前,这会打印出以下内容

  stringone: 1,2,3,4,6,7,8,
  stringtwo: 3,5,6,7,
  stringthree: 2,3,4,5,

我希望它打印出的是

  stringone: 1,2,3,4,6,7,8
  stringtwo: 3,5,6,7
  stringthree: 2,3,4,5

如何检查内部地图内地图的末尾? 任何帮助将不胜感激谢谢

Currently I have a map that prints out the following

map<string, map<int,int> > mapper;
map<int,int>::iterator inner;
map<string, map<int,int> >::iterator outer;


for(outer = mapper.begin(); outer != mapper.end(); outer++){
    cout<<outer->first<<": ";
  for(inner = outer->second.begin(); inner != outer->second.end(); inner++){
      cout<<inner->first<<","<<inner->second<<",";
  }
}

As of now this prints out the following

  stringone: 1,2,3,4,6,7,8,
  stringtwo: 3,5,6,7,
  stringthree: 2,3,4,5,

What i want it to print out is

  stringone: 1,2,3,4,6,7,8
  stringtwo: 3,5,6,7
  stringthree: 2,3,4,5

how can i check for the end of the map inside my inner map?
Any help would be appreciated Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

笨死的猪 2024-09-13 04:20:26

更改输出行以首先打印逗号,并且仅当它不是第一个元素时:

if (inner != outer->second.begin())
    std::cout << ",";
std::cout << inner->first << "," << inner->second;

Change your output line to print the comma first and only if it is not the first element:

if (inner != outer->second.begin())
    std::cout << ",";
std::cout << inner->first << "," << inner->second;
幻想少年梦 2024-09-13 04:20:26

我将其更改为使用 std::copy,我将其与 infix_ostream_iterator

编辑:我还要记录一下,您所做的事情看起来非常像 std::multimap。如果您不想使用多重贴图,看起来仍然是更简单的方法,如下所示:

std::ostream &operator<<(std::ostream &os, std::vector<int> const &v) { 
    std::copy(v.begin(), v.end(),
              infix_ostream_iterator<int>(os, ","));
    return os;
}

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

std::copy(mapper.begin(), mapper.end(), 
          std::ostream_iterator<std::vector<int> >(std::cout, "\n");

I'd change it to use std::copy, which I'd use along with an infix_ostream_iterator.

Edit: I'd also note for the record that what you're doing looks an awful lot like an std::multimap. If you don't want to use a multimap, it still looks like a lot simpler way to go would be something like:

std::ostream &operator<<(std::ostream &os, std::vector<int> const &v) { 
    std::copy(v.begin(), v.end(),
              infix_ostream_iterator<int>(os, ","));
    return os;
}

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

std::copy(mapper.begin(), mapper.end(), 
          std::ostream_iterator<std::vector<int> >(std::cout, "\n");
小耗子 2024-09-13 04:20:26
  for(inner = m->second.begin(); inner != m->second.end(); inner++){ 
      cout<<inner->first<<","<<inner->second;
      if (next(inner) != m->second.end()){
          cout<<",";
      }
  } 

编辑:有人指出,map::iterator不支持加法,合适的替代品是非标准next函数。我更新了上面的代码,这是我自己的 next 版本。

template<typename T>
T next(T incrementable)
{
    return ++incrementable;
}
  for(inner = m->second.begin(); inner != m->second.end(); inner++){ 
      cout<<inner->first<<","<<inner->second;
      if (next(inner) != m->second.end()){
          cout<<",";
      }
  } 

Edit: It has been pointed out that map::iterator doesn't support addition, and that a suitable substitute is a nonstandard next function. I've updated the above code, and here's my own version of next.

template<typename T>
T next(T incrementable)
{
    return ++incrementable;
}
携君以终年 2024-09-13 04:20:26

例如,您可以将结果存储在字符串中,并修剪 for 之后的最后一个字符。

You could store the result in a string for example and trim the last character after the for.

感受沵的脚步 2024-09-13 04:20:26

您需要修改内部循环,以便它在除第一次之外的每次迭代的开头打印逗号分隔符,如下所示:

// inside the loop
cout << ( inner == outer->second.begin() ? "" : "," ) << inner->first << "," << inner->second;

You need to modify your inner loop so that it prints the comma separator at the start for each iteration other than the first, something like:

// inside the loop
cout << ( inner == outer->second.begin() ? "" : "," ) << inner->first << "," << inner->second;
姐不稀罕 2024-09-13 04:20:26

将您的内循环更改为:

if(outter->second.size()) {
    inner = outter->second.begin();
    while(true) {
        cout<<inner->first<<","<<inner->second;
        if(++inner != outer->second.end()) {
            cout<<",";
        } else {
            break;
        }
    }
}

Change your inner loop to this:

if(outter->second.size()) {
    inner = outter->second.begin();
    while(true) {
        cout<<inner->first<<","<<inner->second;
        if(++inner != outer->second.end()) {
            cout<<",";
        } else {
            break;
        }
    }
}
南城旧梦 2024-09-13 04:20:26

您已指定如何从技术上解决问题“找到循环的末尾”,但您尝试解决的格式可以通过其他方式进行管理。

展开内环,然后移动分隔符。

for(m = mapper.begin(); m != mapper.end(); m++){
    cout<<m->first<<": ";

  if( inner = m->second.begin() != m->second.end() ) {
    cout<<inner->first<<","<<inner->second;
    ++inner
    for(; inner != m->second.end(); ++inner){
      cout<<","<<inner->first<<","<<inner->second;
    }
  }
}

这意味着您不必在循环的每次迭代中进行 in if 测试

You have specified how you want to solve your problem technically 'finding the end of the loop' but the formatting you are trying to resolve can be managed in other ways.

Unroll the inner loop, and move the seperator.

for(m = mapper.begin(); m != mapper.end(); m++){
    cout<<m->first<<": ";

  if( inner = m->second.begin() != m->second.end() ) {
    cout<<inner->first<<","<<inner->second;
    ++inner
    for(; inner != m->second.end(); ++inner){
      cout<<","<<inner->first<<","<<inner->second;
    }
  }
}

This means you don't have to do a in if test in each iteration of the loop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文