Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用getline()读入单引号_C++_Getline - Fatal编程技术网

C++ 使用getline()读入单引号

C++ 使用getline()读入单引号,c++,getline,C++,Getline,我正在做UVa问题10082,我正在试着读入一些示例输入来测试我的解决方案。然而,当我阅读文本时,''CCC会输出;;XXX。请注意,只有2个分号,因为输入中有3个单引号,所以应该有3个分号。为什么getline()会忽略第一个单引号 这是我的密码: #include <iostream> #include <string> using namespace std; char mapToWertyu(char c) { char newC = c; c

我正在做UVa问题10082,我正在试着读入一些示例输入来测试我的解决方案。然而,当我阅读文本时,
''CCC
会输出
;;XXX
。请注意,只有2个分号,因为输入中有3个单引号,所以应该有3个分号。为什么getline()会忽略第一个单引号

这是我的密码:

#include <iostream>
#include <string>

using namespace std;

char mapToWertyu(char c)
{
    char newC = c;
    char wertyu[] = {'1','2','3','4','5','6','7','8','9','0','-','=',
                     'Q','W','E','R','T','Y','U','I','O','P','[',']','\\',
                     'A','S','D','F','G','H','J','K','L',';','\'',
                     'Z','X','C','V','B','N','M',',','.','/'};
    char qwerty[] = {'~','1','2','3','4','5','6','7','8','9','0','-','=',
                     'Q','W','E','R','T','Y','U','I','O','P','[',']','\\',
                     'A','S','D','F','G','H','J','K','L',';','\'',
                     'Z','X','C','V','B','N','M',',','.','/'};
    if(c=='A' || c=='Q' || c=='Z')
        return c;

    for(int i = 0; i < 47; ++i)
    {
        if(wertyu[i]==c)
        {
            newC = qwerty[i];
            break;
        }
    }
    return newC;
}

int main()
{
    string input;
    while(cin.peek()!=-1)
    {
        getline(cin,input);
        for(int i = 0; i < input.length(); ++i)
        {
            if(input[i]!='\\')
                cout << mapToWertyu(input[i]);
        }
        cin.ignore(1,'\n');
        cout << endl;
    }
    return 0;
}
#包括
#包括
使用名称空间std;
char mapToWertyu(char c)
{
char newC=c;
字符wertyu[]={'1','2','3','4','5','6','7','8','9','0','-','=',',
"Q","W","E","R","T","Y","U","I","O",
‘A’、‘S’、‘D’、‘F’、‘G’、‘H’、‘J’、‘K’、‘L’、’;’、‘D’、‘F’、‘G’、‘H’、‘H’、‘J’、‘K’、‘L’、‘L’、‘D’、‘D’、‘F’、‘G’、‘G’、‘H’、‘H’、‘J’,
‘Z’、‘X’、‘C’、‘V’、‘B’、‘N’、‘M’、‘M’、‘s’、‘C’、‘V’、‘B’、‘N’、‘M’、‘M’、‘s’、‘s’、‘s’、‘s’、‘s’/’等;
字符qwerty[]={'~','1','2','3','4','5','6','7','8','9','0','-','=',,
"Q","W","E","R","T","Y","U","I","O",
‘A’、‘S’、‘D’、‘F’、‘G’、‘H’、‘J’、‘K’、‘L’、’;’、‘D’、‘F’、‘G’、‘H’、‘H’、‘J’、‘K’、‘L’、‘L’、‘D’、‘D’、‘F’、‘G’、‘G’、,
‘Z’、‘X’、‘C’、‘V’、‘B’、‘N’、‘M’、‘M’、‘s’、‘C’、‘V’、‘B’、‘N’、‘M’、‘M’、‘s’、‘s’、‘s’、‘s’、‘s’/’等;
如果(c='A'| | c=='Q'| | c=='Z')
返回c;
对于(int i=0;i<47;++i)
{
if(wertyu[i]==c)
{
newC=qwerty[i];
打破
}
}
返回newC;
}
int main()
{
字符串输入;
while(cin.peek()!=-1)
{
getline(cin,输入);
对于(int i=0;i不能,因为你告诉它。你做什么事呢,
“\n”)
如果不忽略字符,则应忽略该字符。
std::getline
提取
'\n'
字符,即使未将其放入 绳子

对于其余部分,您没有正确输入。对于初学者,
std::cin.peek()
将在 范围
[0…UCHAR\u MAX]
EOF
EOF
通常被定义为
-1
,但这当然不能保证。但更一般地说: 为什么不使用通常的习语:

while ( std::getline( std::cin, input ) ) {
    //  ...
}
每次创建时,您还可以在
mapToWertyu
中构造数组 叫它吧。那绝对不是你想做的。你可以 仅使用一个静态数组,由字符直接索引, bug这会使程序依赖于编码 但是,请使用两个阵列:

static char const wertyu[] = { ... };
static char const qwerty[] = { ... };
char const* pos = std::find( std::begin( wertyu ), std::end( wertyu ), c );
return pos == std::end( wertyu )
    ? c
    : qwerty[ pos - std::begin( wertyu ) ];
以一种更简单的方式解决问题 如果不需要,需要使用特殊情况
'A'
'Q'
'Z'
要对它们进行转码,请不要将它们放在表中。)

或者

这样做的优点是使精确的贴图可见

或者,如果您100%确信永远不必担心线程:

struct Map
{
    char from;
    char to;
};
static Map map[] =
{
    { '1', '~' },
    { '2', '1' },
    //  ...
    {  0,   0  }
};
Map* sentinal = std::end( map ) - 1;
sentinal->to = sentinal->from = c;
Map const* pos = std::find_if( std::begin( map ), std::end( map ),
                    []( Map const& entry ) { return c == entry.from; } );
return pos->to;
通过插入sentinal,您可以确保条目将 找到了

或者您可以对地图进行排序,并使用
std::lower\u bound

另外,当函数映射时,为什么要调用函数
mapToWertyu

要查询?

我将您的代码剪切粘贴到VS2013中并运行它。在该系统上“CCC生产”;;;XXX@Dweeberly只是一个猜测,但他遇到问题的输入可能不是第一行。除了第一行,他忽略了每行的第一个字符。解决眼前的问题,这真是一个愚蠢的错误,就像我对迈塞尔说的那样f“记住删除
cin.ignore()
”对于其余的答案,不是说这是什么借口,而是我只是把这些代码快速地一起写了一个答案。我很高兴你写了这篇文章,因为我对C++所提供的一切都不太熟悉,而且你已经提出了更好的解决这个问题的方法。实际上,我只是记得我本来打算用一张地图,但是我是你。ght将两个数组放在一起会更快。@rar为了方便起见,我使用了lambdas(即C++11)。在C++11之前的版本中,您必须将lambdas中的代码移到一个单独的函数对象中。(我经常这样做,因此我有一个模板
StaticMap
,它为我定义了函数对象。)
struct Map
{
    char from;
    char to;
};
static Map map[] =
{
    { '1', '~' },
    { '2', '1' },
    //  ...
    {  0,   0  }
};
Map* sentinal = std::end( map ) - 1;
sentinal->to = sentinal->from = c;
Map const* pos = std::find_if( std::begin( map ), std::end( map ),
                    []( Map const& entry ) { return c == entry.from; } );
return pos->to;