Solved

string comparison using std::basic_string

Posted on 2003-10-26
9
275 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 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 119
Which Linux flavors will this run on? 6 102
object oriented programming comparison 5 86
I could not set window to top 4 44
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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