Solved

string comparison using std::basic_string

Posted on 2003-10-26
9
270 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
 

Expert Comment

by:gdean
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:havman56
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

6 Experts available now in Live!

Get 1:1 Help Now