相关疑难解决方法(0)

为什么"使用命名空间std"被认为是不好的做法?

我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::coutstd::cin直接代替.

为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?

c++ namespaces using-directives std c++-faq

2486
推荐指数
36
解决办法
78万
查看次数

如何在C中将字符串转换为整数?

我试图找出是否有一种在C中将字符串转换为整数的替代方法.

我经常在我的代码中模式化以下内容.

char s[] = "45";

int num = atoi(s);
Run Code Online (Sandbox Code Playgroud)

那么,有更好的方式或其他方式吗?

c string atoi

238
推荐指数
6
解决办法
92万
查看次数

如何检查C++字符串是否为int?

当我使用时getline,我会输入一串字符串或数字,但我只想让while循环输出"word",如果它不是数字.那么有没有办法检查"word"是否是一个数字?我知道我可以atoi()用于C字符串,但字符串类的字符串怎么样?

int main () {
  stringstream ss (stringstream::in | stringstream::out);
  string word;
  string str;
  getline(cin,str);
  ss<<str;
  while(ss>>word)
    {
      //if(    )
        cout<<word<<endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ string types stringstream

37
推荐指数
5
解决办法
12万
查看次数

标签 统计

c++ ×2

string ×2

atoi ×1

c ×1

c++-faq ×1

namespaces ×1

std ×1

stringstream ×1

types ×1

using-directives ×1