Clearing Object Duplicates In a vector produces infinite loop











up vote
0
down vote

favorite












I have a vector called:



vector<MiniPair> miniPairVector;


MiniPair object has 2 property inside,1 is an integer docNumber other is a string word



I am trying to clear duplicates in this vector which means that if docNumber and word exist in another object inside vector remove the duplicates



This is what i have tried but it is producing an infinite loop:



for (int i = 0; i < miniPairVector.size(); i++) {

for (int k = i + 1; k < miniPairVector.size(); k++) {

if (miniPairVector[i].getDocNumber() == miniPairVector[k].getDocNumber() && miniPairVector[i].getWord() == miniPairVector[k].getWord()) {
cout << "i am erasing" << endl;
miniPairVector.erase(miniPairVector.begin() + k);

}

}

}


this is the minipair class:



#pragma once
// classes example
#ifndef MINIPAIR_H
#define MINIPAIR_H
#include <iostream>
using namespace std;

class MiniPair {
friend bool operator<(MiniPair const &a, MiniPair const &b) {

return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {

return a.docNumber == b.docNumber && a.word == b.word;
}
private:
string word;
int docNumber;

public:
MiniPair();
MiniPair(string word, int docNumber);
string getWord();
int getDocNumber();

};
#endif









share|improve this question
























  • Use std::unique. Your implémentation is not safe. Implement correctly operator < for MiniPair...
    – PilouPili
    Nov 11 at 22:02












  • This is a fairly inefficient way of going about this.
    – Omnifarious
    Nov 11 at 22:03










  • So if miniPairVector contained n elements, your code will have O(n^2) complexity. Imagine if n is 1000.
    – PaulMcKenzie
    Nov 11 at 22:03












  • thats true thats why it is failing.i never used unique by the way
    – lastpeony4
    Nov 11 at 22:08












  • @lastpeony4 -- Does the order matter? If not, then you can just simply std::sort the vector, and use algorithm functions such as std::unique / vector.erase to remove the duplicates.
    – PaulMcKenzie
    Nov 11 at 22:11

















up vote
0
down vote

favorite












I have a vector called:



vector<MiniPair> miniPairVector;


MiniPair object has 2 property inside,1 is an integer docNumber other is a string word



I am trying to clear duplicates in this vector which means that if docNumber and word exist in another object inside vector remove the duplicates



This is what i have tried but it is producing an infinite loop:



for (int i = 0; i < miniPairVector.size(); i++) {

for (int k = i + 1; k < miniPairVector.size(); k++) {

if (miniPairVector[i].getDocNumber() == miniPairVector[k].getDocNumber() && miniPairVector[i].getWord() == miniPairVector[k].getWord()) {
cout << "i am erasing" << endl;
miniPairVector.erase(miniPairVector.begin() + k);

}

}

}


this is the minipair class:



#pragma once
// classes example
#ifndef MINIPAIR_H
#define MINIPAIR_H
#include <iostream>
using namespace std;

class MiniPair {
friend bool operator<(MiniPair const &a, MiniPair const &b) {

return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {

return a.docNumber == b.docNumber && a.word == b.word;
}
private:
string word;
int docNumber;

public:
MiniPair();
MiniPair(string word, int docNumber);
string getWord();
int getDocNumber();

};
#endif









share|improve this question
























  • Use std::unique. Your implémentation is not safe. Implement correctly operator < for MiniPair...
    – PilouPili
    Nov 11 at 22:02












  • This is a fairly inefficient way of going about this.
    – Omnifarious
    Nov 11 at 22:03










  • So if miniPairVector contained n elements, your code will have O(n^2) complexity. Imagine if n is 1000.
    – PaulMcKenzie
    Nov 11 at 22:03












  • thats true thats why it is failing.i never used unique by the way
    – lastpeony4
    Nov 11 at 22:08












  • @lastpeony4 -- Does the order matter? If not, then you can just simply std::sort the vector, and use algorithm functions such as std::unique / vector.erase to remove the duplicates.
    – PaulMcKenzie
    Nov 11 at 22:11















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a vector called:



vector<MiniPair> miniPairVector;


MiniPair object has 2 property inside,1 is an integer docNumber other is a string word



I am trying to clear duplicates in this vector which means that if docNumber and word exist in another object inside vector remove the duplicates



This is what i have tried but it is producing an infinite loop:



for (int i = 0; i < miniPairVector.size(); i++) {

for (int k = i + 1; k < miniPairVector.size(); k++) {

if (miniPairVector[i].getDocNumber() == miniPairVector[k].getDocNumber() && miniPairVector[i].getWord() == miniPairVector[k].getWord()) {
cout << "i am erasing" << endl;
miniPairVector.erase(miniPairVector.begin() + k);

}

}

}


this is the minipair class:



#pragma once
// classes example
#ifndef MINIPAIR_H
#define MINIPAIR_H
#include <iostream>
using namespace std;

class MiniPair {
friend bool operator<(MiniPair const &a, MiniPair const &b) {

return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {

return a.docNumber == b.docNumber && a.word == b.word;
}
private:
string word;
int docNumber;

public:
MiniPair();
MiniPair(string word, int docNumber);
string getWord();
int getDocNumber();

};
#endif









share|improve this question















I have a vector called:



vector<MiniPair> miniPairVector;


MiniPair object has 2 property inside,1 is an integer docNumber other is a string word



I am trying to clear duplicates in this vector which means that if docNumber and word exist in another object inside vector remove the duplicates



This is what i have tried but it is producing an infinite loop:



for (int i = 0; i < miniPairVector.size(); i++) {

for (int k = i + 1; k < miniPairVector.size(); k++) {

if (miniPairVector[i].getDocNumber() == miniPairVector[k].getDocNumber() && miniPairVector[i].getWord() == miniPairVector[k].getWord()) {
cout << "i am erasing" << endl;
miniPairVector.erase(miniPairVector.begin() + k);

}

}

}


this is the minipair class:



#pragma once
// classes example
#ifndef MINIPAIR_H
#define MINIPAIR_H
#include <iostream>
using namespace std;

class MiniPair {
friend bool operator<(MiniPair const &a, MiniPair const &b) {

return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {

return a.docNumber == b.docNumber && a.word == b.word;
}
private:
string word;
int docNumber;

public:
MiniPair();
MiniPair(string word, int docNumber);
string getWord();
int getDocNumber();

};
#endif






c++ vector






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 2:07









Jeremy Friesner

38.1k1078159




38.1k1078159










asked Nov 11 at 21:55









lastpeony4

135




135












  • Use std::unique. Your implémentation is not safe. Implement correctly operator < for MiniPair...
    – PilouPili
    Nov 11 at 22:02












  • This is a fairly inefficient way of going about this.
    – Omnifarious
    Nov 11 at 22:03










  • So if miniPairVector contained n elements, your code will have O(n^2) complexity. Imagine if n is 1000.
    – PaulMcKenzie
    Nov 11 at 22:03












  • thats true thats why it is failing.i never used unique by the way
    – lastpeony4
    Nov 11 at 22:08












  • @lastpeony4 -- Does the order matter? If not, then you can just simply std::sort the vector, and use algorithm functions such as std::unique / vector.erase to remove the duplicates.
    – PaulMcKenzie
    Nov 11 at 22:11




















  • Use std::unique. Your implémentation is not safe. Implement correctly operator < for MiniPair...
    – PilouPili
    Nov 11 at 22:02












  • This is a fairly inefficient way of going about this.
    – Omnifarious
    Nov 11 at 22:03










  • So if miniPairVector contained n elements, your code will have O(n^2) complexity. Imagine if n is 1000.
    – PaulMcKenzie
    Nov 11 at 22:03












  • thats true thats why it is failing.i never used unique by the way
    – lastpeony4
    Nov 11 at 22:08












  • @lastpeony4 -- Does the order matter? If not, then you can just simply std::sort the vector, and use algorithm functions such as std::unique / vector.erase to remove the duplicates.
    – PaulMcKenzie
    Nov 11 at 22:11


















Use std::unique. Your implémentation is not safe. Implement correctly operator < for MiniPair...
– PilouPili
Nov 11 at 22:02






Use std::unique. Your implémentation is not safe. Implement correctly operator < for MiniPair...
– PilouPili
Nov 11 at 22:02














This is a fairly inefficient way of going about this.
– Omnifarious
Nov 11 at 22:03




This is a fairly inefficient way of going about this.
– Omnifarious
Nov 11 at 22:03












So if miniPairVector contained n elements, your code will have O(n^2) complexity. Imagine if n is 1000.
– PaulMcKenzie
Nov 11 at 22:03






So if miniPairVector contained n elements, your code will have O(n^2) complexity. Imagine if n is 1000.
– PaulMcKenzie
Nov 11 at 22:03














thats true thats why it is failing.i never used unique by the way
– lastpeony4
Nov 11 at 22:08






thats true thats why it is failing.i never used unique by the way
– lastpeony4
Nov 11 at 22:08














@lastpeony4 -- Does the order matter? If not, then you can just simply std::sort the vector, and use algorithm functions such as std::unique / vector.erase to remove the duplicates.
– PaulMcKenzie
Nov 11 at 22:11






@lastpeony4 -- Does the order matter? If not, then you can just simply std::sort the vector, and use algorithm functions such as std::unique / vector.erase to remove the duplicates.
– PaulMcKenzie
Nov 11 at 22:11














2 Answers
2






active

oldest

votes

















up vote
3
down vote













My presumption is that you are doing this for a class.



First, while this may not be relevant for the problem you're solving write now because of class imposed constraints, this is a poor way of implementing this. When implemented correctly the number of comparisons will be something like miniPairVector.size() * miniPairVector.size(). That's a lot of comparisons, and way more than you actually need.



If I were trying to do this in a non-toy (or non-assignment) program, I would use the <algorithm> section of the standard library. I would use ::std::sort and then ::std::unique.



Here's how I would do it using those two:



#include <algorithm>

void remove_dupes(::std::vector<MiniPair> &minipair_vec)
{
::std::sort(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return (a.getDocNumber() < b.getDocNumber())
|| ((a.getDocNumber() == b.getDocNumber())
&& (a.getWord() < b.getWord())));
}); // End lambda and sort.
auto newend = ::std::unique(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return a.getDocNumber() == b.getDocNumber()
&& a.getWord() == b.getWord();
}); // End lambda and unique.
minipair_vec.resize(newend - minipair_vec.begin());
}


I have tested it, so it should work just fine.



The general lesson is that if you find yourself looping, go through this set of questions:




  • Am I indexing into a linear data structure? If so, why am I using indexes instead of iterators?

  • Is there an algorithm that already does what I need, or can a couple of algorithms be easily composed to do what I need?


The code I presented should run in a time that's proportional to minipair_vec.size() * ::std::log2(minipair_vec.size()). The code you wrote would run in a time proportional to minipair_vec.size() * minipair_vec.size() (once you got it to work), which is a lot longer for a large list.






share|improve this answer



















  • 2




    The sort lambda could just be return std::tie(a.getDocNumber(), a.getWord()) < std::tie(b.getDocNumber(), b.getWord());
    – PaulMcKenzie
    Nov 11 at 22:26










  • @PaulMcKenzie - It could be. I didn't think of that, and I'm a little reluctant to add it. Given the nature of the question I would like things to be as clear as possible, and I feel that adds requiring to know about ::std::tie.
    – Omnifarious
    Nov 11 at 22:29


















up vote
2
down vote













A C++98 solution:



#include <algorithm>
#include <string>
#include <vector>

struct MiniPair {
int docNumber;
std::string word;
friend bool operator<(MiniPair const &a, MiniPair const &b) {
return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {
return a.docNumber == b.docNumber && a.word == b.word;
}
};

int main() {
std::vector<MiniPair> miniPairVector;
// fill miniPairVector with data
std::sort(miniPairVector.begin(), miniPairVector.end());
miniPairVector.erase(std::unique(miniPairVector.begin(), miniPairVector.end()), miniPairVector.end());
}





share|improve this answer





















  • Are you sure you can declare a friend inline like that? Your solution is a lot cleaner than mine is. The biggest disadvantage is that it basically defines a global ordering for MiniPair which may or may not be appropriate.
    – Omnifarious
    Nov 11 at 22:34












  • i tried this it is not working non of the dublicates are deleted vector size is same
    – lastpeony4
    Nov 11 at 23:03










  • True @Omnifarious, but you can always override the ordering when needed with a functor. And, yes, inline friends make it work very well with many compilers even in the template case.
    – Bo R
    Nov 11 at 23:16










  • hi i edited question added MiniPair class can you check
    – lastpeony4
    Nov 11 at 23:18










  • @lastpeony4 That dependes on the data. As the code is written it assumes that the pair together are the key. But if only, e.g. docNumber is the key, then change the operator<' and operator== functions to reflect that.
    – Bo R
    Nov 11 at 23:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253642%2fclearing-object-duplicates-in-a-vector-produces-infinite-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













My presumption is that you are doing this for a class.



First, while this may not be relevant for the problem you're solving write now because of class imposed constraints, this is a poor way of implementing this. When implemented correctly the number of comparisons will be something like miniPairVector.size() * miniPairVector.size(). That's a lot of comparisons, and way more than you actually need.



If I were trying to do this in a non-toy (or non-assignment) program, I would use the <algorithm> section of the standard library. I would use ::std::sort and then ::std::unique.



Here's how I would do it using those two:



#include <algorithm>

void remove_dupes(::std::vector<MiniPair> &minipair_vec)
{
::std::sort(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return (a.getDocNumber() < b.getDocNumber())
|| ((a.getDocNumber() == b.getDocNumber())
&& (a.getWord() < b.getWord())));
}); // End lambda and sort.
auto newend = ::std::unique(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return a.getDocNumber() == b.getDocNumber()
&& a.getWord() == b.getWord();
}); // End lambda and unique.
minipair_vec.resize(newend - minipair_vec.begin());
}


I have tested it, so it should work just fine.



The general lesson is that if you find yourself looping, go through this set of questions:




  • Am I indexing into a linear data structure? If so, why am I using indexes instead of iterators?

  • Is there an algorithm that already does what I need, or can a couple of algorithms be easily composed to do what I need?


The code I presented should run in a time that's proportional to minipair_vec.size() * ::std::log2(minipair_vec.size()). The code you wrote would run in a time proportional to minipair_vec.size() * minipair_vec.size() (once you got it to work), which is a lot longer for a large list.






share|improve this answer



















  • 2




    The sort lambda could just be return std::tie(a.getDocNumber(), a.getWord()) < std::tie(b.getDocNumber(), b.getWord());
    – PaulMcKenzie
    Nov 11 at 22:26










  • @PaulMcKenzie - It could be. I didn't think of that, and I'm a little reluctant to add it. Given the nature of the question I would like things to be as clear as possible, and I feel that adds requiring to know about ::std::tie.
    – Omnifarious
    Nov 11 at 22:29















up vote
3
down vote













My presumption is that you are doing this for a class.



First, while this may not be relevant for the problem you're solving write now because of class imposed constraints, this is a poor way of implementing this. When implemented correctly the number of comparisons will be something like miniPairVector.size() * miniPairVector.size(). That's a lot of comparisons, and way more than you actually need.



If I were trying to do this in a non-toy (or non-assignment) program, I would use the <algorithm> section of the standard library. I would use ::std::sort and then ::std::unique.



Here's how I would do it using those two:



#include <algorithm>

void remove_dupes(::std::vector<MiniPair> &minipair_vec)
{
::std::sort(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return (a.getDocNumber() < b.getDocNumber())
|| ((a.getDocNumber() == b.getDocNumber())
&& (a.getWord() < b.getWord())));
}); // End lambda and sort.
auto newend = ::std::unique(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return a.getDocNumber() == b.getDocNumber()
&& a.getWord() == b.getWord();
}); // End lambda and unique.
minipair_vec.resize(newend - minipair_vec.begin());
}


I have tested it, so it should work just fine.



The general lesson is that if you find yourself looping, go through this set of questions:




  • Am I indexing into a linear data structure? If so, why am I using indexes instead of iterators?

  • Is there an algorithm that already does what I need, or can a couple of algorithms be easily composed to do what I need?


The code I presented should run in a time that's proportional to minipair_vec.size() * ::std::log2(minipair_vec.size()). The code you wrote would run in a time proportional to minipair_vec.size() * minipair_vec.size() (once you got it to work), which is a lot longer for a large list.






share|improve this answer



















  • 2




    The sort lambda could just be return std::tie(a.getDocNumber(), a.getWord()) < std::tie(b.getDocNumber(), b.getWord());
    – PaulMcKenzie
    Nov 11 at 22:26










  • @PaulMcKenzie - It could be. I didn't think of that, and I'm a little reluctant to add it. Given the nature of the question I would like things to be as clear as possible, and I feel that adds requiring to know about ::std::tie.
    – Omnifarious
    Nov 11 at 22:29













up vote
3
down vote










up vote
3
down vote









My presumption is that you are doing this for a class.



First, while this may not be relevant for the problem you're solving write now because of class imposed constraints, this is a poor way of implementing this. When implemented correctly the number of comparisons will be something like miniPairVector.size() * miniPairVector.size(). That's a lot of comparisons, and way more than you actually need.



If I were trying to do this in a non-toy (or non-assignment) program, I would use the <algorithm> section of the standard library. I would use ::std::sort and then ::std::unique.



Here's how I would do it using those two:



#include <algorithm>

void remove_dupes(::std::vector<MiniPair> &minipair_vec)
{
::std::sort(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return (a.getDocNumber() < b.getDocNumber())
|| ((a.getDocNumber() == b.getDocNumber())
&& (a.getWord() < b.getWord())));
}); // End lambda and sort.
auto newend = ::std::unique(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return a.getDocNumber() == b.getDocNumber()
&& a.getWord() == b.getWord();
}); // End lambda and unique.
minipair_vec.resize(newend - minipair_vec.begin());
}


I have tested it, so it should work just fine.



The general lesson is that if you find yourself looping, go through this set of questions:




  • Am I indexing into a linear data structure? If so, why am I using indexes instead of iterators?

  • Is there an algorithm that already does what I need, or can a couple of algorithms be easily composed to do what I need?


The code I presented should run in a time that's proportional to minipair_vec.size() * ::std::log2(minipair_vec.size()). The code you wrote would run in a time proportional to minipair_vec.size() * minipair_vec.size() (once you got it to work), which is a lot longer for a large list.






share|improve this answer














My presumption is that you are doing this for a class.



First, while this may not be relevant for the problem you're solving write now because of class imposed constraints, this is a poor way of implementing this. When implemented correctly the number of comparisons will be something like miniPairVector.size() * miniPairVector.size(). That's a lot of comparisons, and way more than you actually need.



If I were trying to do this in a non-toy (or non-assignment) program, I would use the <algorithm> section of the standard library. I would use ::std::sort and then ::std::unique.



Here's how I would do it using those two:



#include <algorithm>

void remove_dupes(::std::vector<MiniPair> &minipair_vec)
{
::std::sort(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return (a.getDocNumber() < b.getDocNumber())
|| ((a.getDocNumber() == b.getDocNumber())
&& (a.getWord() < b.getWord())));
}); // End lambda and sort.
auto newend = ::std::unique(minipair_vec.begin(), minipair_vec.end(),
(MiniPair const &a, MiniPair const &b) -> bool {
return a.getDocNumber() == b.getDocNumber()
&& a.getWord() == b.getWord();
}); // End lambda and unique.
minipair_vec.resize(newend - minipair_vec.begin());
}


I have tested it, so it should work just fine.



The general lesson is that if you find yourself looping, go through this set of questions:




  • Am I indexing into a linear data structure? If so, why am I using indexes instead of iterators?

  • Is there an algorithm that already does what I need, or can a couple of algorithms be easily composed to do what I need?


The code I presented should run in a time that's proportional to minipair_vec.size() * ::std::log2(minipair_vec.size()). The code you wrote would run in a time proportional to minipair_vec.size() * minipair_vec.size() (once you got it to work), which is a lot longer for a large list.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 1:55

























answered Nov 11 at 22:13









Omnifarious

39.9k1196157




39.9k1196157








  • 2




    The sort lambda could just be return std::tie(a.getDocNumber(), a.getWord()) < std::tie(b.getDocNumber(), b.getWord());
    – PaulMcKenzie
    Nov 11 at 22:26










  • @PaulMcKenzie - It could be. I didn't think of that, and I'm a little reluctant to add it. Given the nature of the question I would like things to be as clear as possible, and I feel that adds requiring to know about ::std::tie.
    – Omnifarious
    Nov 11 at 22:29














  • 2




    The sort lambda could just be return std::tie(a.getDocNumber(), a.getWord()) < std::tie(b.getDocNumber(), b.getWord());
    – PaulMcKenzie
    Nov 11 at 22:26










  • @PaulMcKenzie - It could be. I didn't think of that, and I'm a little reluctant to add it. Given the nature of the question I would like things to be as clear as possible, and I feel that adds requiring to know about ::std::tie.
    – Omnifarious
    Nov 11 at 22:29








2




2




The sort lambda could just be return std::tie(a.getDocNumber(), a.getWord()) < std::tie(b.getDocNumber(), b.getWord());
– PaulMcKenzie
Nov 11 at 22:26




The sort lambda could just be return std::tie(a.getDocNumber(), a.getWord()) < std::tie(b.getDocNumber(), b.getWord());
– PaulMcKenzie
Nov 11 at 22:26












@PaulMcKenzie - It could be. I didn't think of that, and I'm a little reluctant to add it. Given the nature of the question I would like things to be as clear as possible, and I feel that adds requiring to know about ::std::tie.
– Omnifarious
Nov 11 at 22:29




@PaulMcKenzie - It could be. I didn't think of that, and I'm a little reluctant to add it. Given the nature of the question I would like things to be as clear as possible, and I feel that adds requiring to know about ::std::tie.
– Omnifarious
Nov 11 at 22:29












up vote
2
down vote













A C++98 solution:



#include <algorithm>
#include <string>
#include <vector>

struct MiniPair {
int docNumber;
std::string word;
friend bool operator<(MiniPair const &a, MiniPair const &b) {
return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {
return a.docNumber == b.docNumber && a.word == b.word;
}
};

int main() {
std::vector<MiniPair> miniPairVector;
// fill miniPairVector with data
std::sort(miniPairVector.begin(), miniPairVector.end());
miniPairVector.erase(std::unique(miniPairVector.begin(), miniPairVector.end()), miniPairVector.end());
}





share|improve this answer





















  • Are you sure you can declare a friend inline like that? Your solution is a lot cleaner than mine is. The biggest disadvantage is that it basically defines a global ordering for MiniPair which may or may not be appropriate.
    – Omnifarious
    Nov 11 at 22:34












  • i tried this it is not working non of the dublicates are deleted vector size is same
    – lastpeony4
    Nov 11 at 23:03










  • True @Omnifarious, but you can always override the ordering when needed with a functor. And, yes, inline friends make it work very well with many compilers even in the template case.
    – Bo R
    Nov 11 at 23:16










  • hi i edited question added MiniPair class can you check
    – lastpeony4
    Nov 11 at 23:18










  • @lastpeony4 That dependes on the data. As the code is written it assumes that the pair together are the key. But if only, e.g. docNumber is the key, then change the operator<' and operator== functions to reflect that.
    – Bo R
    Nov 11 at 23:19















up vote
2
down vote













A C++98 solution:



#include <algorithm>
#include <string>
#include <vector>

struct MiniPair {
int docNumber;
std::string word;
friend bool operator<(MiniPair const &a, MiniPair const &b) {
return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {
return a.docNumber == b.docNumber && a.word == b.word;
}
};

int main() {
std::vector<MiniPair> miniPairVector;
// fill miniPairVector with data
std::sort(miniPairVector.begin(), miniPairVector.end());
miniPairVector.erase(std::unique(miniPairVector.begin(), miniPairVector.end()), miniPairVector.end());
}





share|improve this answer





















  • Are you sure you can declare a friend inline like that? Your solution is a lot cleaner than mine is. The biggest disadvantage is that it basically defines a global ordering for MiniPair which may or may not be appropriate.
    – Omnifarious
    Nov 11 at 22:34












  • i tried this it is not working non of the dublicates are deleted vector size is same
    – lastpeony4
    Nov 11 at 23:03










  • True @Omnifarious, but you can always override the ordering when needed with a functor. And, yes, inline friends make it work very well with many compilers even in the template case.
    – Bo R
    Nov 11 at 23:16










  • hi i edited question added MiniPair class can you check
    – lastpeony4
    Nov 11 at 23:18










  • @lastpeony4 That dependes on the data. As the code is written it assumes that the pair together are the key. But if only, e.g. docNumber is the key, then change the operator<' and operator== functions to reflect that.
    – Bo R
    Nov 11 at 23:19













up vote
2
down vote










up vote
2
down vote









A C++98 solution:



#include <algorithm>
#include <string>
#include <vector>

struct MiniPair {
int docNumber;
std::string word;
friend bool operator<(MiniPair const &a, MiniPair const &b) {
return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {
return a.docNumber == b.docNumber && a.word == b.word;
}
};

int main() {
std::vector<MiniPair> miniPairVector;
// fill miniPairVector with data
std::sort(miniPairVector.begin(), miniPairVector.end());
miniPairVector.erase(std::unique(miniPairVector.begin(), miniPairVector.end()), miniPairVector.end());
}





share|improve this answer












A C++98 solution:



#include <algorithm>
#include <string>
#include <vector>

struct MiniPair {
int docNumber;
std::string word;
friend bool operator<(MiniPair const &a, MiniPair const &b) {
return a.docNumber < b.docNumber || a.docNumber == b.docNumber && a.word < b.word;
}
friend bool operator==(MiniPair const &a, MiniPair const &b) {
return a.docNumber == b.docNumber && a.word == b.word;
}
};

int main() {
std::vector<MiniPair> miniPairVector;
// fill miniPairVector with data
std::sort(miniPairVector.begin(), miniPairVector.end());
miniPairVector.erase(std::unique(miniPairVector.begin(), miniPairVector.end()), miniPairVector.end());
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 22:29









Bo R

616110




616110












  • Are you sure you can declare a friend inline like that? Your solution is a lot cleaner than mine is. The biggest disadvantage is that it basically defines a global ordering for MiniPair which may or may not be appropriate.
    – Omnifarious
    Nov 11 at 22:34












  • i tried this it is not working non of the dublicates are deleted vector size is same
    – lastpeony4
    Nov 11 at 23:03










  • True @Omnifarious, but you can always override the ordering when needed with a functor. And, yes, inline friends make it work very well with many compilers even in the template case.
    – Bo R
    Nov 11 at 23:16










  • hi i edited question added MiniPair class can you check
    – lastpeony4
    Nov 11 at 23:18










  • @lastpeony4 That dependes on the data. As the code is written it assumes that the pair together are the key. But if only, e.g. docNumber is the key, then change the operator<' and operator== functions to reflect that.
    – Bo R
    Nov 11 at 23:19


















  • Are you sure you can declare a friend inline like that? Your solution is a lot cleaner than mine is. The biggest disadvantage is that it basically defines a global ordering for MiniPair which may or may not be appropriate.
    – Omnifarious
    Nov 11 at 22:34












  • i tried this it is not working non of the dublicates are deleted vector size is same
    – lastpeony4
    Nov 11 at 23:03










  • True @Omnifarious, but you can always override the ordering when needed with a functor. And, yes, inline friends make it work very well with many compilers even in the template case.
    – Bo R
    Nov 11 at 23:16










  • hi i edited question added MiniPair class can you check
    – lastpeony4
    Nov 11 at 23:18










  • @lastpeony4 That dependes on the data. As the code is written it assumes that the pair together are the key. But if only, e.g. docNumber is the key, then change the operator<' and operator== functions to reflect that.
    – Bo R
    Nov 11 at 23:19
















Are you sure you can declare a friend inline like that? Your solution is a lot cleaner than mine is. The biggest disadvantage is that it basically defines a global ordering for MiniPair which may or may not be appropriate.
– Omnifarious
Nov 11 at 22:34






Are you sure you can declare a friend inline like that? Your solution is a lot cleaner than mine is. The biggest disadvantage is that it basically defines a global ordering for MiniPair which may or may not be appropriate.
– Omnifarious
Nov 11 at 22:34














i tried this it is not working non of the dublicates are deleted vector size is same
– lastpeony4
Nov 11 at 23:03




i tried this it is not working non of the dublicates are deleted vector size is same
– lastpeony4
Nov 11 at 23:03












True @Omnifarious, but you can always override the ordering when needed with a functor. And, yes, inline friends make it work very well with many compilers even in the template case.
– Bo R
Nov 11 at 23:16




True @Omnifarious, but you can always override the ordering when needed with a functor. And, yes, inline friends make it work very well with many compilers even in the template case.
– Bo R
Nov 11 at 23:16












hi i edited question added MiniPair class can you check
– lastpeony4
Nov 11 at 23:18




hi i edited question added MiniPair class can you check
– lastpeony4
Nov 11 at 23:18












@lastpeony4 That dependes on the data. As the code is written it assumes that the pair together are the key. But if only, e.g. docNumber is the key, then change the operator<' and operator== functions to reflect that.
– Bo R
Nov 11 at 23:19




@lastpeony4 That dependes on the data. As the code is written it assumes that the pair together are the key. But if only, e.g. docNumber is the key, then change the operator<' and operator== functions to reflect that.
– Bo R
Nov 11 at 23:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253642%2fclearing-object-duplicates-in-a-vector-produces-infinite-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values