Solved

string comparison using std::basic_string

Posted on 2003-10-26
9
274 Views
Last Modified: 2013-12-14
I am new at using STLs. i have a set<string>. When I iterate over it and get the string, how do i compare it with a const string& in my function. i get the error that it cannot compare a std::basic_string<> with a string. How do i do this??
0
Comment
Question by:shyam_sg
9 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 125 total points
ID: 9624527
Hey shyam_sg

It all depends on the compiler and the stl you're using

According to Microsoft Documentation you can do

string s = "MyString1";
string t = "MyString1";

int i = t.compare( s );

returning a positive value, 0 or a negative value depending on which one is "bigger"

Hope this helps

Tincho

0
 
LVL 9

Expert Comment

by:tinchos
ID: 9624542
An example

#include <iostream>
#include <string>

using namespace std;

int main()
{
      string s = "MyString1";
      string t = "MyString2";

      int i = t.compare( s );

      cout << i << endl;
}

string s = "MyString1";
string t = "MyString2";
This will return a positive value as s < t

string s = "MyString2";
string t = "MyString1";
This will return a negative value as s > t

string s = "MyString1";
string t = "MyString1";
This will return 0 as s == t


Hope I've been clear enough

Tincho
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9625104
>> When I iterate over it and get the string, how do i compare it with a const string& in my function. i get the error that it
>> cannot compare a std::basic_string<> with a string.

basic_string< char > is the same thing as string because of the following type definition,

typedef basic_string< char > string;

Therefore, there is nothing wrong with comparing a string to a basic string<char>. You chould post your code.

basic_string's compare function returns a value representing the lexicographical relationship b etween the two strings. You can also use the usual comparison operators. For example,

string s1 = "this is a test';
string s2 = "abcdefghijklmn";

if( s1 > s2 )
    cout << "s1 is greater than s2. " << endl;
else
    cout << "s1 is less than s2" << endl;

or,

if( s1 < "Sdfsdfasdf" )
    cout << "s1 is less than a random string." << endl;

In like manner, the following logical operator overloads are also provided,

==
!=
<=
>=

Exceter
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Expert Comment

by:gdean
ID: 9626177
Here is another example.  I included the strCompare struct, this would normally be needed if the < operator was not defined for the type your using.  However as Exceter pointed out it is defined for strings.  Just thought it may be useful at some point.

Perhaps the more pertinent part of the example is the iterators.  That should pretty much sum everything up on how to compare the contents of set<string>.

#include <set>
#include <iostream>
#include <string>

using namespace std;

struct strCompare
{
      bool operator()(const string& LHS, const string& RHS)
      {      return bool(LHS<RHS);}
};

int main()
{
      set<string,strCompare> S;

      S.insert("a string");
      S.insert("another string");
      S.insert("yet another string");
      S.insert("1 2 3 4 5");
      S.insert("@_()");

      set<string,strCompare>::iterator it;

      for(it= S.begin();it!=S.end();it++)
      {
            set<string,strCompare>::iterator it2;

            for(it2= S.begin();it2!=S.end();it2++)
            {
                  if((*it)<(*it2))
                        cerr << (*it) << " < " << (*it2) << endl;
                  else if((*it)>(*it2))
                        cerr << (*it)<< " < " << (*it2) << endl;
                  else
                        cerr << (*it)<< " == " << (*it2) << endl;
            }
      }

    system("pause");
      return 0;
}
0
 
LVL 4

Expert Comment

by:havman56
ID: 9626896
std::basic_string<char> is same as string

if u compare std::basic_string<T> with string u may get an error
bcoz if T is a wchar_t then u cannot compare with string
then u need wstring for it

check what ur having as atemplate
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9626956
True but due to the following,

>> i have a set<string>. When I iterate over it and get the string, how do i compare it with a const string& in my function.

I think the asker is using the basic_string< char> type definition.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546173
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in approximately one week.  I would appreciate any comments by the experts that would help me in making a recommendation.

It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:
http://www.experts-exchange.com/help.jsp#hs5

Tinchos
EE Cleanup Volunteer
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10586349
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: tinchos {http:#9624527}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 91
Header of docx file 17 115
c++, dynamic object by json 1 53
Gaming Software 1 19
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question