首页 新闻 会员 周边

std::string 与 #include<string>

0
悬赏园豆:20 [已解决问题] 解决于 2018-07-28 19:13
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main(void)
{
    char chars1[20];
    char chars2[20] = "jaguar";
    string str1;
    string str2 = "pather";
    cout << "Enter a kind of feline: " << endl;
    cin >> chars1;
    cout << "Enter another kind of feline: " << endl;
    cin >> str1;
    cin.get();
    return 0;
}

上面的程序如果没有#include<string>,则cin>>str1将会报错,而string str1不会报错

也就是说在没有#include<string>的情况下,是可以定义std::string变量的,只是不能使用cin进行输入。

 

到底在没有头文件<string>的时候,std::string 是个什么东东,怎么理解?

注销111的主页 注销111 | 初学一级 | 园豆:46
提问于:2018-07-26 19:49
< >
分享
最佳答案
0

那是因为对 string 的 >> 运算符重载是在 string 头文件中定义的;所以没有 string 头文件,就不能使用 >> 对 string 类型变量操作了

收获园豆:11
Arthurian | 小虾三级 |园豆:1123 | 2018-07-26 20:21
其他回答(1)
0

可以定义std::string变量,是因为引入了iostream头,关于>>运算符,1楼已经说了

收获园豆:9
jello chen | 园豆:7306 (大侠五级) | 2018-07-26 21:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册