gpt4 book ai didi

C++ 查找并保存 vector 中的重复项

转载 作者:行者123 更新时间:2023-11-27 22:39:33 27 4
gpt4 key购买 nike

我有一个用户定义类型 vector 的自定义 vector

第一个 vector 通过 stdin 填充元素,然后我对其进行排序并尝试在其中找到重复项并保存它们

我已经设法找到了所有独特的元素,但我需要找到并获得一个重复的 vector 我需要一个提示或一个简单的解决方案来解决这个问题

下面是我的代码:

攻击者.h

#ifndef Agressor_h
#define Agressor_h

#include <string>
#include <vector>

using namespace std;

class Agressor{
public:
/*const char**/ string traderIdentifier;
/*const char**/ string side;
int quantity;
int price;
vector<Agressor> v;

void display(){
cout << traderIdentifier << " " << side << " " << quantity << " " << price << endl;
}
explicit Agressor(){

}
~Agressor(){

}
friend ostream &operator<<(ostream& stream, const Agressor& item);
const friend bool operator > (const Agressor &a1, const Agressor &a2);

// const friend bool operator == (const Agressor &a1, const Agressor &a2);

/* vector<Agressor>& operator[](int i ){
return v[i];
}*/


};


ostream &operator<<(ostream& stream, const Agressor& item) {
string side = "";
if(item.side == "B"){
side = '+';
}else{
if(item.side == "S"){
side = "-";
}
}

stream << item.traderIdentifier << side << item.quantity << "@" << item.price << "\n";
return stream;
}

const bool operator == (const Agressor &a1, const Agressor &a2){
bool isEqual = false;
if((a1.price*a1.quantity == a2.price*a2.quantity) && (a1.traderIdentifier == a2.traderIdentifier) && (a1.side == a2.side)){
isEqual = true;
}
return(isEqual);
}


const bool operator > (const Agressor &a1, const Agressor &a2){
bool isGreater = false;
if(a1.price*a1.quantity > a2.price*a2.quantity){
isGreater = true;
}
return(isGreater);
}



#endif /* Agressor_h */

main.cpp

#include <iostream>
#include "Agressor.h"
#include <sstream>
using namespace std;


vector<string> &split(const string &s, char delim, vector<string> &elems)
{
stringstream ss(s);
string item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}

vector<string> split(const string &s, char delim)
{
vector<string> elems;
split(s, delim, elems);
return elems;
}

bool equal_comp(const Agressor& a1, const Agressor& a2){
if((a1.price*a1.quantity == a2.price*a2.quantity) && (a1.traderIdentifier == a2.traderIdentifier) && (a1.side == a2.side)){
return true;
}
return false;
}

int main(int argc, const char * argv[]) {
Agressor agr;
while (true) {
std::string sText;
cout << "enter query:" << endl;
std::getline(std::cin, sText);
if(sText == "q"){
cout << "Program terminated by user" << endl;
break;
}else{
std::vector<std::string> sWords = split(sText, ' ');
agr.traderIdentifier = sWords[0];
agr.side = sWords[1];
agr.quantity = stoi(sWords[2]);
agr.price = stoi(sWords[3]);
agr.v.push_back(agr);

vector<Agressor>::iterator it;
sort(agr.v.begin(), agr.v.end(), greater<Agressor>());
//unique(agr.v.begin(), agr.v.end(), equal_comp);
for (vector<Agressor>::const_iterator i = agr.v.begin(); i != agr.v.end(); ++i)
cout << *i << ' ';

}
}
cout << "here we go..." << endl;
vector<Agressor>::iterator it;
sort(agr.v.begin(), agr.v.end(), greater<Agressor>());
//it = unique(agr.v.begin(),agr.v.end(), equal_comp);
//agr.v.resize( distance(agr.v.begin(),it) );
agr.v.erase(unique(agr.v.begin(),agr.v.end(), equal_comp), agr.v.end());

copy(agr.v.begin(), agr.v.end(), ostream_iterator<Agressor>(cout, "\n"));



return 0;
}

最佳答案

你可以使用类似的东西:

template <typename T>
std::vector<T> get_duplicates(const std::vector<T>& v)
{
// expect sorted vector

auto it = v.begin();
auto end = v.end();
std::vector<T> res;
while (it != end) {
it = std::adjacent_find(it, end);
if (it != end) {
++it;
res.push_back(*it);
}
}
return res;
}

关于C++ 查找并保存 vector 中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50152163/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com