list class from STL
-
Hello everybody, Could anybody let me know how to use unique member function of list template class in STL? My problem is, I have a C++ class, Complex, which has two data members, real and imaginary numbers. I am using Complex class in creating a list. How can I use unique member function of list class when I have created a list using Complex class? Grateful to you, Thanks, :) Software Developer Sanjay Khapre
-
Hello everybody, Could anybody let me know how to use unique member function of list template class in STL? My problem is, I have a C++ class, Complex, which has two data members, real and imaginary numbers. I am using Complex class in creating a list. How can I use unique member function of list class when I have created a list using Complex class? Grateful to you, Thanks, :) Software Developer Sanjay Khapre
list::unique works on sorted ranges, so that might explain why you think it doesnt work. #include #include //#include using namespace std; struct Complex { Complex(int a, int b): i(a), r(b) {} int i; int r; bool operator < (const Complex&c) { if (r != c.r) return r < c.r; else return i < c.i; } bool operator == (const Complex&c) { return r == c.r && i == c.i; } friend ostream& operator << (const ostream&out, const Complex& c); }; ostream& operator << (ostream&out, const Complex& c) { out << "(" << c.r << "," << c.i << ")"; return out; } int main() { list l; for (unsigned int i = 0;i<10;++i) l.push_back(Complex(1+i%2,2+(i+1)%3)); copy(l.begin(), l.end(), ostream_iterator(cout, "\t")); cout << endl; l.unique(); // unsorted list, chances are that no elements are equal to its preceding neighbor copy(l.begin(), l.end(), ostream_iterator(cout, "\t")); cout << endl; l.sort(); copy(l.begin(), l.end(), ostream_iterator(cout, "\t")); cout << endl; l.unique(); copy(l.begin(), l.end(), ostream_iterator(cout, "\t")); cout << endl; return 0; } --- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot
-
Hello everybody, Could anybody let me know how to use unique member function of list template class in STL? My problem is, I have a C++ class, Complex, which has two data members, real and imaginary numbers. I am using Complex class in creating a list. How can I use unique member function of list class when I have created a list using Complex class? Grateful to you, Thanks, :) Software Developer Sanjay Khapre
#include #include "boost/assign.hpp" namespace { template struct complex_less : public std::binary_function, std::complex, bool> { bool operator()(const std::complex& rLhs, const std::complex& rRhs) const { //complex numbers don't have '<', force one here if (rLhs.real() != rRhs.real()) { return (rLhs.real() < rRhs.real()); } return (rLhs.imag() < rRhs.imag()); } }; } void TestMain() { std::list< std::complex > lst; lst = boost::assign::list_of(std::complex(1, 1)) (std::complex(2, 2)) (std::complex(2, 2)) (std::complex(0, 0)); lst.sort(complex_less()); lst.unique(); #ifdef _DEBUG std::copy(lst.begin(), lst.end(), std::ostream_iterator< std::complex >(std::cout, " ")); std::cout << std::endl; #endif }