LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2004, 04:43 AM   #1
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Rep: Reputation: 15
Convert C++ string to C char*


I use a StringIntMap in my C++ programm, it's defined like this:
typedef map<string,int> StringIntMap;

Now when I am iterating though this map, I want to use the strstr() function to search for a substring in the keys of the map. This part of the program looks like that:

StringIntMap::iterator q;
StringIntMap ph;
for (q=ph.begin();q!=ph.end();++q) {
if (strstr(q->first,k)) {r=true;}
}

k is another string. The problem is, strstr() needs char* !!
How do I convert the string type to a char* pointer that strstr can use?
 
Old 06-17-2004, 05:01 AM   #2
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Original Poster
Rep: Reputation: 15
ah, it probably works wih the .c_str() method...
 
Old 06-17-2004, 05:01 AM   #3
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
you can use the find of string :
if (q->first.find(k)) ...

or you can use c_str:
if (strstr(q->first.c_str(),k))
 
Old 06-17-2004, 08:15 AM   #4
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Original Poster
Rep: Reputation: 15
thanks, you're right, .find() is the best way to do this...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ : replacing a char in a string cb951303 Programming 20 02-25-2015 08:18 PM
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
How to convert string to char? twirl Programming 27 10-13-2005 07:11 AM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration