如果我注释掉换行符,为什么'string'会成为一个不合格的变量
Why would 'string' become an ineligible variable if I comment out Newline
在这里完成新手,我有一个关于代码中换行符的快速问题。所以我知道插入using namespace std
是不好的做法,在编写我的程序时,我避免使用cout
和cin
,而不先添加std::
部分。但我想既然我没有导入命名空间库,我可以将其注释掉。但是当我这样做时,我的变量string
名称变得无法识别(它下面的红线(。当我允许再次导入命名空间时,红线消失了。变量string
是否仅在命名空间库中可用?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
//using namespace std;
// read and compare names
int main()
{
std::cout << "Please enter two names n";
string first;
string second;
std:: cin >> first >> second; // read two strings
if (first == second) std::cout << "that's the same name twice! n";
if (first < second) std::cout << first << " is alphabetically before "
<< second << 'n';
if (first > second) std::cout << first << " is alphabetically after " <<
second << 'n';
return 0;
}
如果你不包括using namespace std
那么你将需要说
std::string first;
std::string second;
因为string
也在standard
命名空间中定义(以及cout
等(。
所以是的,你是对的,string
只在standard
中定义.string
是一个对象(不是基元类型(,正是这一点允许您进行if(first==second)
比较。否则,比较字符串的"正常"方法是使用strcmp()
或类似。
相关文章:
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- vs 2015 constexpr变量不恒定,但与2019相比还好吗
- 方法内部但循环仍得到预期的不合格id错误C++
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 在头文件中定义变量不会出错
- 如果我注释掉换行符,为什么'string'会成为一个不合格的变量
- 为什么我的数组值与此处的全局变量不匹配?
- 静态成员变量不会由 gettext 转换
- 是否允许类类型的 std::function 成员变量(不完整类型)?
- 变量不在 lambda 的范围内
- constexpr 函数在编译时获取值,即使我的变量不是 constexpr
- 为什么当我使用 std::string::operator[] 时自动变量不会成为引用?
- Constexpr变量不是编译时值
- 表达式必须具有常数值,变量不能用作定义数组大小的常数
- 为什么我的 std::atomic<int> 变量不是线程安全的?
- 变量不能明确
- 为什么非成员静态 constexpr 变量不是隐式内联的?
- C++中的不合格查找
- 流变量不能出现在 OpenMP 第一私有中?
- 为什么 UInt64 变量不能包含大于 UInt32::Max 的值?