我#include <string>对程序开始时的使用感到困惑.例如,在下面的代码中,我不使用,#include <string>但该函数在运行时仍会打印出字符串"Johnny的最爱号码".
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
Run Code Online (Sandbox Code Playgroud)
但是,在下面的代码中,它确实包含#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;
}
Run Code Online (Sandbox Code Playgroud)
我只#include <string>在变量表示字符串时使用吗?