vector unique problem

Pages: 12
I'm SORRY people. It was the actual code I have been working on so I removed it but now I have written an example which is working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <utility>
#include <cassert>
#include <fstream>
#include <stdio.h>

struct LC_413
{
	LC_413() {}
	
    std::string x;
    std::string a;
    std::string y;
    std::string z;
    std::string e;
    std::string b;
    std::string c;

    LC_413( const std::string& x, const std::string& a, const std::string& y, const std::string& z, const std::string& e, const std::string& b, 
    const std::string& c): x(x), a(a), y(y), z(z), e(e), b(b), c(c) {}
    
    // "A", "w", "k", "917", "B", "g", "p"
    bool operator < (const LC_413 & rhs) const
    {
        if ( x < rhs.x ) return true;
        if ( x > rhs.x ) return false;

        if ( a < rhs.a ) return true;
        if ( a > rhs.a ) return false;

        if ( y < rhs.y ) return true;
        if ( y > rhs.y ) return false;

        return z < rhs.z;
    }

};

struct only_unique
{
    explicit only_unique() {}

    bool operator() ( const LC_413 & a , const LC_413& b ) const
    { 
         return !(a.x==b.x && a.z!=b.z && a.e!=b.e);
    }
};

std::ostream& operator<< ( std::ostream& stm, const LC_413& a )
{ return stm << '[' << a.x << ", " << a.a << ", " << a.y << ", " << a.z << ", " << a.e << ", " << a.b << ", " << a.c << ']' ; }

int main()
{
    std::vector<LC_413> v1;

    v1.push_back(LC_413("A", "w", "k", "917", "B", "g", "p"));
    v1.push_back(LC_413("A", "x", "l", "917", "B", "h", "q"));
    v1.push_back(LC_413("A", "y", "m", "916", "S", "i", "r"));
    v1.push_back(LC_413("A", "z", "n", "916", "S", "j", "s"));
    
    std::stable_sort (v1.begin(),v1.end());
    v1.erase( std::unique(v1.begin(),v1.end(),only_unique()) , v1.end());
    
    for(std::vector<LC_413>::iterator itr = v1.begin(); itr != v1.end(); ++itr)
    {
    	std::cout << *itr << std::endl;
    }
}
Topic archived. No new replies allowed.
Pages: 12