Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I use '#include <string>' at the start of a C++ program?

Tags:

c++

string

I am confused about the use of #include <string> at the start of a program. For example, in the code below, I don't use #include <string> but the function will still print out the string "Johnny's favorite number is" when it is run.

#include <iostream>
using namespace std;

void printVariable(int number){
    cout << "Johnny's favorite number is" << number << endl
}

However, in this code below, it does contain #include <string>.

#include <iostream>
#include <string>
using namespace std;

class Var{
    public:
        void setName(string x){
            name = x;
        }

        string getName(){
           return name;
        }
   private:
       string name;
};

int main(){
    Var Classy;
    Classy.setName("Johnny Bravo");
    cout << Classy.getName() << endl;
    return 0;
}

Do I only use #include <string> if a variable represents a string?

like image 905
xai Avatar asked Dec 08 '14 01:12

xai


People also ask

When should a semicolon be used examples?

A semicolon can replace a period if the writer wishes to narrow the gap between two closely linked sentences (independent clauses). Examples: Call me tomorrow; you can give me an answer then. We have paid our dues; we expect all the privileges listed in the contract.

When to use a colon or a semicolon?

Semicolons should introduce evidence or a reason for the preceding statement; for example, this sentence appropriately uses a semicolon. A colon, on the other hand, should be used for a stronger, more direct relationship. It should provide emphasis, an example, or an explanation.

How do you use commas and semicolons?

When a comma separates two complete sentences joined by a conjunction (and, but, or, nor, for, so, or yet) the comma and the conjunction can be replaced with a semicolon. I ate dinner, and I went to the movies. = I ate dinner; I went to the movies. She finished top of her class, but she was struggling to find work.


3 Answers

Do I only use #include <string> if a variable represents a string?

Yes.

Use #include <string> when you use a variable that has type std::string.

The code "text here", contrary to intuition, is not a std::string; it is a string literal, and a C-style string, and a const char[10] convertible to const char*. Welcome to C++ with its legacy oddities.

like image 112
Lightness Races in Orbit Avatar answered Nov 15 '22 00:11

Lightness Races in Orbit


If you use the type std::string in your code then you should include the <string> header. There are also a few other types and functions in that header, but std::string is the most commonly used one.

However, you do not need to include this header just to use string literals, which are built into the core language.

like image 30
Brian Bi Avatar answered Nov 14 '22 23:11

Brian Bi


Your question arises from the fact that you know that something like "aabcd" is a string literal. So, its type should be string. Well, that's not quite true.

C++ has a lot of features from C. Including data types. So, that is a pointer to char (char*), not a string(an instance of the string class). You can create an instance of the string class from a char* (including a string literal) by passing it as argument to the constructor of string. But it is not a string, it's just some misleading terminology.

A similar case is calling things vectors when they are arrays.

like image 42
Paul92 Avatar answered Nov 15 '22 00:11

Paul92