相关疑难解决方法(0)

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

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

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

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

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

std :: cin用空格输入?

#include <string>

std::string input;
std::cin >> input;
Run Code Online (Sandbox Code Playgroud)

用户想要输入"Hello World".但是cin在两个词之间的空间失败了.我怎样才能cin完全接受Hello World

我实际上是用结构做这个,cin.getline似乎不起作用.这是我的代码:

struct cd
{
    std::string CDTitle[50];
    std::string Artist[50];
    int number_of_songs[50];
};

std::cin.getline(library.number_of_songs[libNumber], 250);
Run Code Online (Sandbox Code Playgroud)

这会产生错误.有任何想法吗?

c++ string space

129
推荐指数
7
解决办法
46万
查看次数

如何使用cin从用户那里读取完整的一行?

这是我目前的C++代码.我想知道如何编写一行代码.我还会使用cin.getline(y)或不同的东西吗?我已经检查过,但找不到任何东西.当我运行它时,它完美地工作,除了它只键入一个单词而不是我需要输出的整行.这是我需要帮助的.我在代码中概述了它.

谢谢你的帮助

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>

using namespace std;

int main()
{
    char x;

    cout << "Would you like to write to a file?" << endl;
    cin >> x;
    if (x == 'y' || x == 'Y')
    {
        char y[3000];
        cout << "What would you like to write." << endl;
        cin >> y;
        ofstream file;
        file.open("Characters.txt");
        file << strlen(y) << " Characters." << endl;
        file << endl;
        file << y; …
Run Code Online (Sandbox Code Playgroud)

c++ iostream

18
推荐指数
3
解决办法
6万
查看次数

标签 统计

c++ ×3

c++-faq ×1

iostream ×1

namespaces ×1

space ×1

std ×1

string ×1

using-directives ×1