I'm trying to learn about strings, but different sources tell my to include different headers.
Some say to use <string.h>, but others mention "apstring.h". I was able to do some basic stuff with apstring, but I've been told the other one is more powerful. When I include <string.h> and try to declare some string variables, however, I get errors. What is the proper usage?
You want to include <string> and use std::string:
#include <string>
#include <iostream>
int main()
{
std::string s = "a string";
std::cout << s << std::endl;
}
But what you really need to do is get an introductory level book. You aren't going to learn properly any other way, certainly not scrapping for information online.
Sources telling you to use apstring.h are materials for the Advanced Placement course in computer science. It describes a string class that you'll use through the course, and some of the exam questions may refer to it and expect you to be moderately familiar with it. Unless you're enrolled in that class or studying to take that exam, ignore those sources.
Sources telling you to use string.h are either not really talking about C++, or are severely outdated. You should probably ignore them, too. That header is for the C functions for manipulating null-terminated arrays of characters, also known as C-style strings.
In C++, you should use the string header. Write #include <string> at the top of your file. When you declare a variable, the type is string, and it's in the std namespace, so its full name is std::string. You can avoid having to write the namespace portion of that name all the time by following the example of lots of introductory texts and saying using namespace std at the top of the C++ source files (but generally not at the top of any header files you might write).
I don't hear about "apstring".If you want to use string with c++ ,you can do like this:
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str;
...
return 0;
}
I hope this can avail
You shouldn't be using string.h if you're coding in C++. Strings in C++ are of the std::string variety which is a lot easier to use than then old C-style "strings". Use:
#include <string>
to get the correct information and something std::string s to declare one. The many wonderful ways you can use std::string can be seen here.
If you have a look at the large number of questions on Stack Overflow regarding the use of C strings, you'll see why you should avoid them where possible :-)
The C++ string class is std::string. To use it you need to include the <string> header.
For the fundamentals of how to use std::string, you'll want to consult a good introductory C++ book.
Maybe this link will help you.
See: std::string documentation.
#include <string> is the most widely accepted.
"apstring" is not standard C++, in C++, you'd want to #include the <string> header.
Use this:
#include <string>
For using the string header first we must have include string header file as #include <string> and then we can include string header in the following ways in C++:
1)
string header = "--- Demonstrates Unformatted Input ---";
2)
string header("**** Counts words****\n"), prompt("Enter a text and terminate"
" with a period and return:"), line( 60, '-'), text;
Related
I'm learning to code using C++. I learned that to use the string datatype you need to include the <string> library, but how does this code still work when I haven't included the <string> library?
#include <iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
It outputs a string to the console without me needing to include the <string> library. Shouldn't it return an error?
There is a difference between a std::string object (that comes from the <string> header file) and a C-style string. You do not need to include the <string> header file in order to use C-style strings, as they are built-in to the language. This is mainly due to the fact that a C-style string is just an array of characters, ie an array of bytes, and is found within the instruction set architecture. std::string objects are more commonly used than C-style strings in C++, due to the methods involved, and their ease of operation. See the following helpful links:
whats the difference between C strings and C++ strings?
std::string vs C-strings
The text "Hello World!\n" is not a string but an array of chars: char[14], which allows you to print to the console without any additional includes.
It is likely being included with iostream
It depends on the compiler.
<iostream> includes <string>
But as I said before it's not necessary.
It depends on the compiler.
I know that we use #include<iostream> for cout function.
However, I'm curious when we need to use #include<string>.
Im a student taking C++ class.
Thanks.
Include it whenever you need something from that header. Here you can find a list of things that you need to #include <string> for.
I am curious if there is any header file in C++ which can be used (included) instead of the standard namespace (namespace std) that works the same even in new versions of C++? I want to know if I can write code without using any namespaces and still be able to use the string data type.
string is in the std namespace, so you can't completely disregard it.
There are options though:
using std::string;
using namespace std;
typedef std::string myString;
//or fully qualify the name
std::string mystr;
which you can put in a header and include that.
There, now I gave you the recipe for disaster. Don't use it!
Namespaces are good. Learn to use them, rather than hacking your way around them.
Headers and namespaces are not related, and namespaces are good things. using namespace std is bad. You can always use the std::string data type without using namespace std;.
To use "using namespace std;" is a poor idea (although I have to admit I do this rather regularly in my samples I post here, for ease of typing). To hide the same in a header file is an even worse idea.
Namespaces are there for a reason.
But if you have, say, 100000 lines of already existing code that is written pre-namespace standard, and you quickly want to port that to use in a new compiler, then adding "using namespace std;" to the top of each file would be the preferred solution.
You could typedef the classes you wish to use, but this is a really bad idea.
#include <string>
typedef std::string string;
I started learning strings and string functions (from a book) , I learned functions like strcpy and strcat and strncat..etc
So I started to practice using them in simple programs to get a sense of what they do.
Then I was surprised later that in the book it tells me that i have to use #include <cstring> in order to use all these string functions.
I have tried using string functions more than once without including <cstring> so why?
The only header file i included was <iostream> and yet i was able to use string functions.
Please someone explain to me why the string functions worked without <cstring> and do I need to include it to use string functions, and if no what are the uses of <cstring>;
First of all, you absolutely need to consider switching to std::string. Manual memory allocation, while being an interesting and sometimes challenging task, should not be a part of your everyday job.
Having said that, probably the <cstring> was #included by some other header you are using in your project. However it's better not to depend on the other headers including <cstring> (no one guarantees that they will do always and for every compiler), and include it where appropriate.
You don't need to include <cstring> because it is included by iostream.
However note that the function you are talking about (strcpy, strcat, strncat) are C function taking char * and have their C++ equivalents working with the more convenient std::string.
strcpy: std::string::operator=
std::string str2;
std::string str1 = str2; // copy str2 in str1
strcat: std::string::operator+=
str1 += str2; // concat str2 to str1
strncat:
str1 += str2.substr(0,n); // concat the first n characters of str2 to str1
That would simply mean that <cstring> was included by <iostream>. When you included <iostream> you also implicitly included <cstring> through it.
Note that it is not generally guaranteed that <cstring> is included by <iostream>. You just got lucky that your specific implementation happened to have that inclusion. In a different implementation that might not be the case.
To all likelihood, your c++ standard library version implements some features of iostream with c standard library functions which are located in cstring. Thus, when you include the iostream header file, this will have an #include directive that gives you cstring.
We've got a reasonable sized C++ application that's pretty old at this stage, so it's got a few quirks.
One of these quirks is in how it deals with C++ compilers that use a pre-standisation standard library. There's one header file that's supposed to resolve any differences between a standards compliant compiler and this one non-compliant compiler. For various reasons we can't / don't want to stop supporting this compiler.
#include <vector>
#include <set>
#if defined(NO_STD_LIB)
#include <iostream.h>
#else
#incude <iostream>
using std::string;
using std::cout;
using std::vector;
using std::cout;
#endif
You use this as follows
#include stl.h
int main() {
vector<string> foo;
.....
return 0;
}
There are 2 main issues with this approach:
Each compilation unit that includes std.h has to compile lots of un-needed code (and we're trying to reduce compile times as much as possible at the minute)
The global namespace gets polluted with pretty much everything that would normally be in the std namespace.
I really would like to address both of these points as part of a code cleanup project. The first one really is the more important reason for doing this.
As we have to support this old compiler then our code will always have to avoid clashing names with things that it exposes in it's standard lib, so point 2 isn't really relevant, though I'd like to see a solution that works when / if we can finally drop support for it.
My idea so far is to break up the super header into a set of smaller headers. e.g. stl_vector, stl_iostream, stl_set, etc. This way we can only include the parts of the standard library that we're interested in. These filenames follow the pattern of the std headers, but with an easily searched for prefix. So when the time comes to dump the offending compiler, it'll be simple to search for the prefix and remove it.
I think that will fix issue 1 easily enough.
My real problem is fixing issue 2. I thought of doing someting like this
#if defined(NO_STD_LIB)
#include <iostream.h>
#define std
#else
#include <iostream>
then we could code as follows:
#incude "stl_iostream"
int main() {
std::string foo("bar");
std::cout << foo << std::endl;
}
And that almost worked. Where there was no standard namespace the #define std made std::string decompose into ::string and life was good.
Then I tried this with a .cc file that used the dreaded "using namespace std;" and I get a compile error because that becomes "using namespace ", so that obviously won't work.
Now obviously I could ban people from writing "using namespace std;", but as much as it should be avoided in headers, it's sometimes useful in .cc files where you're making heavy use of lots of STL classes.
So, finally, to the question. Is there a standard idiom for dealing with this issue. Or if there isn't a standard way to deal with this, then what tricks do you use to support compilers that use a pre-standard standard library.
I've thought of using pre-compiled headers to solve the compilation speed issue, but we target different compilers and the effort of getting this working across all of them may mean its not worth our time doing it.
Answers that advise me to drop the non-conforming compiler may be popular, but won't be accepted as this is something that we can't do just now.
You can try:
#if defined(NO_STD_LIB)
namespace std {
using ::string;
using ::cout;
using ::vector;
using ::cout;
}
#endif
Then std::string will work.
It would have been much better if the using namespace ::; directive existed in the language; however it doesn't.