LinuxQuestions.org
Review your favorite Linux distribution.
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 01-20-2006, 11:13 AM   #1
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Rep: Reputation: 15
Strings in C++


Hello everyone,

What is the difference between:
#include <string>, #include <string.h>, #include <String.h>.
I'm so confused about C++ string. Which one am I suppose to use in C++.

Can anybody just give description on how to get this to work, In some files #include <string.h> works for me fine, and in other files I had to put #include <string>. Which one is right for C++?

Thanks,
Alaa G
 
Old 01-20-2006, 11:33 AM   #2
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
this is deprecated for C++...
Code:
#include <string.h>
...so simply use always
Code:
#include <string>

Last edited by Flesym; 01-20-2006 at 11:36 AM.
 
Old 01-20-2006, 11:33 AM   #3
Ha1f
Member
 
Registered: Jun 2005
Location: University of Maryland
Distribution: FreeBSD
Posts: 268

Rep: Reputation: 30
In C++ you use #include <string>. <string.h> is from the standard C library, which means it's also available in C.
 
Old 01-20-2006, 11:34 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
#include <string> c++ std::string
#include <string.h> c string lib
#include <cstring> for using c string in c++

<string>
ex.
string s = "this is a c++ string";
s += "appending this";

<string.h>
strcpy, strcmp, strcat ect.. C functions..

<cstring>
use the string.h inside of c++ the proper way..

hth
 
Old 01-20-2006, 11:38 AM   #5
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks guys,

What is String.h with capital "S"?

Alaa G
 
Old 01-20-2006, 11:38 AM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by Flesym
this is deprecated...
Code:
#include <string.h>
...so simply use always
Code:
#include <string>
string.h is not deprecated.. it is the C string lib.. if you use
<string>
and
<string.h>
you get two separate things..


<string.h>
and
<cstring>
are the same thing but the proper way to use string.h in c++ is with
<cstring>
 
Old 01-20-2006, 11:40 AM   #7
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
Also sometimes I have #include<string.h> and C++ string works and I could do all string manipulation, but not all the time. Why is this happening?

How do you convert from char* to string?

Alaa G
 
Old 01-20-2006, 11:44 AM   #8
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
>> How do you convert from char* to string?
Code:
char* a = "hello";
string b(a);
also i think the difference in String.h and string.h may be none.. i am not at home to try it, but in devcpp i get no errors when i do something like
#include <IoStReAm>

so maybe the case is not mattering..
I have not used String.h that is any different that i know of..
 
Old 01-20-2006, 11:47 AM   #9
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks xhi,
One more question, sorry i'm kinda new in C++. whenever you include in c++, you don't add .h, right?

and if I want to include something from C, do I add extern "C" {#include <something_in_C.h>}?
 
Old 01-20-2006, 11:48 AM   #10
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
also operator=
Code:
char* a = "hello";
string b;
b=a;
that is why std::string is so nice..
 
Old 01-20-2006, 11:52 AM   #11
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Quote:
string.h is not deprecated.. it is the C string lib.. if you use
string.h is(!) deprecated (at least for C++, I had already added this extra-piece in my earlier post), it is not part of C++ standard any more (once it was) -> so deprecated. But the problem is solved anyway, so who cares... ;-)
 
Old 01-20-2006, 11:57 AM   #12
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by allomeen
Thanks xhi,
One more question, sorry i'm kinda new in C++. whenever you include in c++, you don't add .h, right?

and if I want to include something from C, do I add extern "C" {#include <something_in_C.h>}?
well not everything, but the new version of the std lib you dont.. so anything like iostream, string, fstream, etc... you do not..
with header files you create you will.. and other files that are not standard like for example from a graphics lib you are using or somthing like that..

i cannot help with the extern C as I have read the definition before, but have not found a use for it.. so I do not have the complete understanding of it..
 
Old 01-20-2006, 12:00 PM   #13
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
string.h is(!) deprecated (at least for C++, I had already added this extra-piece in my earlier post), it is not part of C++ standard any more (once it was) -> so deprecated. But the problem is solved anyway, so who cares... ;-)
the way you put it is not correct
Quote:
Originally Posted by Flesym
this is deprecated for C++...
Code:
#include <string.h>
...so simply use always
Code:
#include <string>
string.h and string are not and were not the same files..

string.h is deprecated for cstring not string

<edit> cstring and string.h contain the same functionality, just a namespace diff </edit>

Last edited by xhi; 01-20-2006 at 12:03 PM.
 
Old 01-20-2006, 12:05 PM   #14
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
Thank you so much guys
 
  


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
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
splitting strings basher400 Linux - Newbie 6 04-12-2005 02:24 AM
strings in c djgerbavore Programming 8 01-11-2005 04:27 PM
pointers and strings skibud2 Programming 4 09-22-2003 07:38 AM
strings with xmodmap? Gantrep Linux - Software 1 07-28-2003 11:39 AM

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

All times are GMT -5. The time now is 04:53 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