Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. list class from STL

list class from STL

Scheduled Pinned Locked Moved ATL / WTL / STL
c++questionhelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SanjaySMK
    wrote on last edited by
    #1

    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

    J G 2 Replies Last reply
    0
    • S SanjaySMK

      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

      J Offline
      J Offline
      Jonas Larsson
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • S SanjaySMK

        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

        G Offline
        G Offline
        Gast128
        wrote on last edited by
        #3

        #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 }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups