Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string comparison using std::basic_string

Posted on 2003-10-26
9
Medium Priority
?
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

609 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