I'm trying to learn about string
s, 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?
This question is related to
c++
string
header-files
"apstring"
is not standard C++, in C++, you'd want to #include
the <string>
header.
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 :-)
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;
Maybe this link will help you.
See: std::string documentation.
#include <string>
is the most widely accepted.
Use this:
#include < string>
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.
Source: Stackoverflow.com