Unable to get std::sort working

Discussion in 'C++' started by Disc Magnet, Mar 3, 2010.

  1. Disc Magnet

    Disc Magnet Guest

    I have written this short piece of code to try out std::sort()
    function.

    #include <vector>
    #include <iostream>
    #include <algorithm>

    using namespace std;

    int main()
    {
    vector<string> lines;

    lines.push_back("apple");
    lines.push_back("mango");
    lines.push_back("bat");
    lines.push_back("cake");
    lines.push_back("acid");
    lines.push_back("brick");
    lines.push_back("base");

    vector<string>::iterator itr;

    cout << "Before sorting" << endl;
    int i = 0;
    for (itr = lines.begin(); itr != lines.end(); itr++) {
    cout << i << ") " << *itr << endl;
    i++;
    }
    cout << endl;

    std::sort (lines.begin(), lines.end());

    cout << "After sorting" << endl;
    i = 0;
    for (itr = lines.begin(); itr != lines.end(); itr++) {
    cout << i << ") " << *itr << endl;
    i++;
    }
    }

    This runs fine when I compile using g++ on Linux. Here is the output:

    $ ./a.out
    Before sorting
    0) apple
    1) mango
    2) bat
    3) cake
    4) acid
    5) brick
    6) base

    After sorting
    0) acid
    1) apple
    2) base
    3) bat
    4) brick
    5) cake
    6) mango

    But if I try to compile this on Visual Studio 2005, I get an error
    like this:

    1>main.cpp
    1>./src/main.cpp(24) : error C2679: binary '<<' : no operator found
    which takes a right-hand operand of type
    'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable
    conversion)
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Ax=std::allocator<char>
    1> ]

    Line 24 is this line: cout << i << ") " << *itr << endl;

    So, I comment out both the cout statements which try to print *itr and
    then I get this error during compilation:

    1>c:/Program Files/Microsoft Visual Studio 8/VC/include/
    algorithm(2677) : error C2784: 'bool std::eek:perator <(const
    std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could
    not deduce template argument for 'const std::vector<_Ty,_Alloc> &'
    from 'std::basic_string<_Elem,_Traits,_Ax>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Ax=std::allocator<char>
    1> ]

    Could someone please help me to get this working?
    Disc Magnet, Mar 3, 2010
    #1
    1. Advertising

  2. On Mar 3, 7:49 pm, Disc Magnet <> wrote:
    > I have written this short piece of code to try out std::sort()
    > function.
    >
    > #include <vector>
    > #include <iostream>
    > #include <algorithm>
    >
    > using namespace std;
    >
    > int main()
    > {
    >     vector<string> lines;
    >
    >     lines.push_back("apple");
    >     lines.push_back("mango");
    >     lines.push_back("bat");
    >     lines.push_back("cake");
    >     lines.push_back("acid");
    >     lines.push_back("brick");
    >     lines.push_back("base");
    >
    >     vector<string>::iterator itr;
    >
    >     cout << "Before sorting" << endl;
    >     int i = 0;
    >     for (itr = lines.begin(); itr != lines.end(); itr++) {
    >         cout << i << ") " << *itr << endl;
    >         i++;
    >     }
    >     cout << endl;
    >
    >     std::sort (lines.begin(), lines.end());
    >
    >     cout << "After sorting" << endl;
    >     i = 0;
    >     for (itr = lines.begin(); itr != lines.end(); itr++) {
    >         cout << i << ") " << *itr << endl;
    >         i++;
    >     }
    >
    > }
    >
    > This runs fine when I compile using g++ on Linux. Here is the output:
    >
    > $ ./a.out
    > Before sorting
    > 0) apple
    > 1) mango
    > 2) bat
    > 3) cake
    > 4) acid
    > 5) brick
    > 6) base
    >
    > After sorting
    > 0) acid
    > 1) apple
    > 2) base
    > 3) bat
    > 4) brick
    > 5) cake
    > 6) mango
    >
    > But if I try to compile this on Visual Studio 2005, I get an error
    > like this:
    >
    > 1>main.cpp
    > 1>./src/main.cpp(24) : error C2679: binary '<<' : no operator found
    > which takes a right-hand operand of type
    > 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable
    > conversion)
    > 1>        with
    > 1>        [
    > 1>            _Elem=char,
    > 1>            _Traits=std::char_traits<char>,
    > 1>            _Ax=std::allocator<char>
    > 1>        ]
    >
    > Line 24 is this line:         cout << i << ") " << *itr << endl;
    > [...]
    > Could someone please help me to get this working?


    You use std::string but didn't include <string>. Apparently gcc
    #includes <string> in one of the other headers you used whereas
    MSVC includes only part of the <string> contents in its headers.
    Your code needs the full definition of <string> for operator<<()
    and operator<(). Therefore: #include it.
    Gert-Jan de Vos, Mar 3, 2010
    #2
    1. Advertising

  3. Disc Magnet

    Disc Magnet Guest

    On Mar 3, 11:58 pm, Gert-Jan de Vos <gert-
    > wrote:
    > On Mar 3, 7:49 pm, Disc Magnet <> wrote:
    >
    >
    >
    >
    >
    > > I have written this short piece of code to try out std::sort()
    > > function.

    >
    > > #include <vector>
    > > #include <iostream>
    > > #include <algorithm>

    >
    > > using namespace std;

    >
    > > int main()
    > > {
    > >     vector<string> lines;

    >
    > >     lines.push_back("apple");
    > >     lines.push_back("mango");
    > >     lines.push_back("bat");
    > >     lines.push_back("cake");
    > >     lines.push_back("acid");
    > >     lines.push_back("brick");
    > >     lines.push_back("base");

    >
    > >     vector<string>::iterator itr;

    >
    > >     cout << "Before sorting" << endl;
    > >     int i = 0;
    > >     for (itr = lines.begin(); itr != lines.end(); itr++) {
    > >         cout << i << ") " << *itr << endl;
    > >         i++;
    > >     }
    > >     cout << endl;

    >
    > >     std::sort (lines.begin(), lines.end());

    >
    > >     cout << "After sorting" << endl;
    > >     i = 0;
    > >     for (itr = lines.begin(); itr != lines.end(); itr++) {
    > >         cout << i << ") " << *itr << endl;
    > >         i++;
    > >     }

    >
    > > }

    >
    > > This runs fine when I compile using g++ on Linux. Here is the output:

    >
    > > $ ./a.out
    > > Before sorting
    > > 0) apple
    > > 1) mango
    > > 2) bat
    > > 3) cake
    > > 4) acid
    > > 5) brick
    > > 6) base

    >
    > > After sorting
    > > 0) acid
    > > 1) apple
    > > 2) base
    > > 3) bat
    > > 4) brick
    > > 5) cake
    > > 6) mango

    >
    > > But if I try to compile this on Visual Studio 2005, I get an error
    > > like this:

    >
    > > 1>main.cpp
    > > 1>./src/main.cpp(24) : error C2679: binary '<<' : no operator found
    > > which takes a right-hand operand of type
    > > 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable
    > > conversion)
    > > 1>        with
    > > 1>        [
    > > 1>            _Elem=char,
    > > 1>            _Traits=std::char_traits<char>,
    > > 1>            _Ax=std::allocator<char>
    > > 1>        ]

    >
    > > Line 24 is this line:         cout << i << ") " << *itr << endl;
    > > [...]
    > > Could someone please help me to get this working?

    >
    > You use std::string but didn't include <string>. Apparently gcc
    > #includes <string> in one of the other headers you used whereas
    > MSVC includes only part of the <string> contents in its headers.
    > Your code needs the full definition of <string> for operator<<()
    > and operator<(). Therefore: #include it.


    Thanks! This solved the problem.
    Disc Magnet, Mar 3, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,275
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    347
    Jim Langston
    Jul 7, 2005
  3. Replies:
    1
    Views:
    478
    Marcus Kwok
    Apr 6, 2007
  4. Stephen Horne
    Replies:
    3
    Views:
    571
    Stephen Horne
    Aug 5, 2009
  5. Navin
    Replies:
    1
    Views:
    674
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page