51

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?

Maxpm
  • 24,113
  • 33
  • 111
  • 170

9 Answers9

100

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.

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
15

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).

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 11
    I'm sorry to hear that, @Inverse, because as I said, it's the method that many introductory texts use because it simplifies the initial C++ experience. Beginners shouldn't have to worry about namespaces right away. For them, it's enough for `using namespace std` to just be an incantation that they can put at the top each program they write. Then, we don't have to explain to them immediately why they have to put `std::` in front of every variable declaration. Non-beginners can learn later what that line really does and why it's not a great idea to use in larger programs. – Rob Kennedy Nov 05 '10 at 15:23
7

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

yubaolee
  • 895
  • 2
  • 8
  • 16
5

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 :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • `string.h` is a C header https://en.cppreference.com/w/cpp/header/cstring – Marek R Aug 08 '23 at 11:11
  • @MarekR, I'm well aware that `string.h` is a C header, but I'm not advocating using that *or* its `cstring` cousin (you can, by the way, use both `cstring` and `string.h` but the latter is deprecated and there's a subtle difference as to what namespace they're guaranteed to populate). Instead, I'm suggesting you use `string` and its *real* C++ strings. – paxdiablo Aug 08 '23 at 14:40
4

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.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
2

Maybe this link will help you.

See: std::string documentation.

#include <string> is the most widely accepted.

Kimbluey
  • 1,199
  • 2
  • 12
  • 23
prolink007
  • 33,872
  • 24
  • 117
  • 185
0

"apstring" is not standard C++, in C++, you'd want to #include the <string> header.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
0

Use this:

#include <string>
Saiansh Singh
  • 583
  • 5
  • 16
fatih
  • 1,171
  • 2
  • 14
  • 26
-3

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;
David
  • 2,663
  • 3
  • 24
  • 41