Solved

string comparison using std::basic_string

Posted on 2003-10-26
9
273 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now