相关疑难解决方法(0)

删除结构c ++向量中的重复项

我有以下结构.我想将结构存储在矢量中.其次我想删除(context)上的重复值.我究竟做错了什么?


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//Structure
struct contextElement
{
  string context;
  float x;
};

int main()
{
  vector<contextElement> v1;
  v1.push_back({"1",1.0});
  v1.push_back({"2",2.0});
  v1.push_back({"1",1.0});
  v1.push_back({"1",1.0});
  //ERROR here
  auto comp = [] ( const contextElement& lhs, const contextElement& rhs ) {return lhs.context == rhs.context;};
  //Remove elements that have the same context
  v1.erase(std::unique(v1.begin(), v1.end(),comp));
  for(size_t i = 0; i < v1.size();i++)
  {
    cout << v1[i].context <<"  ";
  }
  cout << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:

main.cpp | …

c++ structure vector unique duplicate-removal

3
推荐指数
1
解决办法
2988
查看次数

标签 统计

c++ ×1

duplicate-removal ×1

structure ×1

unique ×1

vector ×1