Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从std::set C++;_C++_Stl - Fatal编程技术网

C++ 从std::set C++;

C++ 从std::set C++;,c++,stl,C++,Stl,您好,我有一组包含以下元素的std::pair: set<pair<string, int> > mapElem; apples 1 apples 2 at 1 eating 1 eating 2 football 1 going 1 today 1 today 2 today 3 today 4 today 5 tommy 1 tommy 2 对于原始数据而言,std::set似乎是一个相当奇怪的容器 无论如何,只需在集合上迭代,并使用std::map存储具有最高值

您好,我有一组包含以下元素的std::pair:

set<pair<string, int> > mapElem;

apples 1
apples 2
at 1
eating 1
eating 2
football 1
going 1
today 1
today 2
today 3
today 4
today 5
tommy 1
tommy 2

对于原始数据而言,
std::set
似乎是一个相当奇怪的容器

无论如何,只需在集合上迭代,并使用
std::map
存储具有最高值的对。对于每个对,如果映射中的对具有较低的值,则更新它

一个普通的for循环会做得很好,不要考虑
for_each
之类的(保持简单)


干杯,

A
std::set
对于原始数据来说似乎是一个相当奇怪的容器

map<string, int> aMap(mapElem.begin(), mapElem.end());
set<pair<string, int> > Temp(aMap.begin(), aMap.end());
mapElem.swap(Temp);
无论如何,只需在集合上迭代,并使用
std::map
存储具有最高值的对。对于每个对,如果映射中的对具有较低的值,则更新它

一个普通的for循环会做得很好,不要考虑
for_each
之类的(保持简单)

干杯,

映射aMap(mapElem.begin(),mapElem.end());
map<string, int> aMap(mapElem.begin(), mapElem.end());
set<pair<string, int> > Temp(aMap.begin(), aMap.end());
mapElem.swap(Temp);
设置温度(aMap.begin(),aMap.end()); mapElem交换(临时);
没有地图:

set<pair<string, int> >::iterator nextit = mapElem.begin();
while (nextit != mapElem.end()) {
  set<pair<string, int> >::iterator it = nextit++;
  if (nextit != mapElem.end()) {
    if (it->first == nextit->first) {
      mapElem.erase(it);
    }
  }
}
set::迭代器nextit=mapElem.begin();
while(nextit!=mapElem.end()){
set::iterator it=nextit++;
if(nextit!=mapElem.end()){
如果(it->first==nextit->first){
删除(它);
}
}
}
映射aMap(mapElem.begin(),mapElem.end());
设置温度(aMap.begin(),aMap.end());
mapElem交换(临时);
没有地图:

set<pair<string, int> >::iterator nextit = mapElem.begin();
while (nextit != mapElem.end()) {
  set<pair<string, int> >::iterator it = nextit++;
  if (nextit != mapElem.end()) {
    if (it->first == nextit->first) {
      mapElem.erase(it);
    }
  }
}
set::迭代器nextit=mapElem.begin();
while(nextit!=mapElem.end()){
set::iterator it=nextit++;
if(nextit!=mapElem.end()){
如果(it->first==nextit->first){
删除(它);
}
}
}

请注意,集合的内容先按字符串排序,然后按整数排序。因此,要删除每个字符串值中除最高值之外的所有条目,请找到具有该字符串值的范围,并删除除最后一个之外的所有条目。类似这样(未经测试):


请注意,集合的内容首先按字符串排序,然后按整数排序。因此,要删除每个字符串值中除最高值之外的所有条目,请找到具有该字符串值的范围,并删除除最后一个之外的所有条目。类似这样(未经测试):


啊,对不起,我忘了说我想不使用地图(不要问为什么):@vBx:为什么我们不能问为什么?了解您的限制对于帮助解决您的问题非常重要!@Jefromi:因为我在做一个项目,项目明确地说不要使用地图,除了地图以外的任何容器不,这样我说不要问,因为这是一个荒谬的想法不使用地图,所以我需要与thsi CODEAH共谋抱歉,我忘了说我想不使用地图(不要问为什么):@vBx:为什么我们不能问为什么?了解您的限制对于帮助解决您的问题非常重要!@杰弗罗米:因为我在做一个项目,项目明确地说不要使用地图,任何容器,但地图不,这样我说不要问,因为这是一个荒谬的想法不使用地图,所以我需要与自己共谋代码添加了一个没有地图的替代添加了一个没有地图的替代