C++ C++;在vector中查找并保存重复项
我有一个自定义向量,属于我的用户定义向量类型 第一个向量通过stdin填充元素,然后我对它进行排序,试图在其中找到重复的元素并保存它们 我已经找到了所有唯一的元素,但是我需要找到并得到一个重复的向量 对于这个问题,我需要一个提示或简单的解决方案 下面是我的代码: Agressor.hC++ C++;在vector中查找并保存重复项,c++,vector,stl,duplicates,unique,C++,Vector,Stl,Duplicates,Unique,我有一个自定义向量,属于我的用户定义向量类型 第一个向量通过stdin填充元素,然后我对它进行排序,试图在其中找到重复的元素并保存它们 我已经找到了所有唯一的元素,但是我需要找到并得到一个重复的向量 对于这个问题,我需要一个提示或简单的解决方案 下面是我的代码: Agressor.h #ifndef Agressor_h #define Agressor_h #include <string> #include <vector> using namespace std
#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 */
\ifndef Agressor\u h
#定义施暴者
#包括
#包括
使用名称空间std;
阶级革命者{
公众:
/*常量字符**/string traderIdentifier;
/*常量字符**/字符串侧;
整数;
国际价格;
向量v;
无效显示(){
cout您可以使用以下内容:
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;
}
模板
std::vector获取重复项(常量std::vector&v)
{
//期望排序向量
自动it=v.begin();
自动结束=v.结束();
std::向量res;
while(it!=结束){
it=std::相邻的查找(it,end);
如果(它!=结束){
++它;
res.push_back(*it);
}
}
返回res;
}
您可以使用以下内容:
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;
}
模板
std::vector获取重复项(常量std::vector&v)
{
//期望排序向量
自动it=v.begin();
自动结束=v.结束();
std::向量res;
while(it!=结束){
it=std::相邻的查找(it,end);
如果(它!=结束){
++它;
res.push_back(*it);
}
}
返回res;
}
std::unique
用以后的非重复值覆盖重复值。您可以实现类似的算法,将值移动到某个位置
template<class ForwardIt, class OutputIt, class BinaryPredicate>
ForwardIt unique_retain(ForwardIt first, ForwardIt last, OutputIt d_first, BinaryPredicate p)
{
if (first == last)
return last;
ForwardIt result = first;
while (++first != last) {
if (!p(*result, *first) && ++result != first) {
*d_first++ = std::move(*result);
*result = std::move(*first);
}
}
return ++result;
}
std::unique
用以后的非重复值覆盖重复值。您可以实现类似的算法,将值移动到某个位置
template<class ForwardIt, class OutputIt, class BinaryPredicate>
ForwardIt unique_retain(ForwardIt first, ForwardIt last, OutputIt d_first, BinaryPredicate p)
{
if (first == last)
return last;
ForwardIt result = first;
while (++first != last) {
if (!p(*result, *first) && ++result != first) {
*d_first++ = std::move(*result);
*result = std::move(*first);
}
}
return ++result;
}
从排序向量中,重复的元素等于上一个元素。从排序向量中,重复的元素等于上一个元素。