Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Unable to get std::sort working

Reply
Thread Tools

Unable to get std::sort working

 
 
Disc Magnet
Guest
Posts: n/a
 
      03-03-2010
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: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?
 
Reply With Quote
 
 
 
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      03-03-2010
On Mar 3, 7:49*pm, Disc Magnet <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Disc Magnet
Guest
Posts: n/a
 
      03-03-2010
On Mar 3, 11:58*pm, Gert-Jan de Vos <gert-
(E-Mail Removed)> wrote:
> On Mar 3, 7:49*pm, Disc Magnet <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to get VoIP QoS working on PIX mhoppes@gmail.com Cisco 2 03-28-2007 02:03 AM
unable to get importNode working yawnmoth Javascript 5 08-23-2006 10:52 AM
AP1200: unable to get back working after update to IOS guru@progon.net Cisco 2 07-02-2005 01:02 PM
Unable to get OMA working Paul Cotgrove ASP .Net Mobile 3 08-20-2004 04:18 PM
unable to get GMP and Perl working... Jasper Perl 0 06-29-2004 02:41 AM



Advertisments