Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.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
检测要在if语句中使用的输入空白 < >我试图创建一个C++程序,它检测到空白时输入了什么代码> CIN < /C> >行,然后检测到哪个单词出现在它后面。 我只是在这里展示我的功能: int lineFunction (string line) { if (/*whatever characters, then whitespace, and the letter ‘a’ is read*/) { return 0; }else if (/*whatever characters, then whitespace followed by the letter ‘b’*/) { return 1; }else { return 2; } }_C++_Whitespace - Fatal编程技术网

检测要在if语句中使用的输入空白 < >我试图创建一个C++程序,它检测到空白时输入了什么代码> CIN < /C> >行,然后检测到哪个单词出现在它后面。 我只是在这里展示我的功能: int lineFunction (string line) { if (/*whatever characters, then whitespace, and the letter ‘a’ is read*/) { return 0; }else if (/*whatever characters, then whitespace followed by the letter ‘b’*/) { return 1; }else { return 2; } }

检测要在if语句中使用的输入空白 < >我试图创建一个C++程序,它检测到空白时输入了什么代码> CIN < /C> >行,然后检测到哪个单词出现在它后面。 我只是在这里展示我的功能: int lineFunction (string line) { if (/*whatever characters, then whitespace, and the letter ‘a’ is read*/) { return 0; }else if (/*whatever characters, then whitespace followed by the letter ‘b’*/) { return 1; }else { return 2; } },c++,whitespace,C++,Whitespace,因此,如果输入是,比如说:abca,那么输出应该是0,如果输入是abcb,那么输出应该是1,否则应该是2。 我还想知道这个函数是否可以“堆叠”,比如我是否可以有多个空格和多个单词。您可以使用和 #包括 #包括 #包括 #包括 int-lineFunction(标准::字符串行){ auto begin=std::find_if(std::begin(line),std::end(line),[](unsigned char c){return std::isspace(c);}); if(beg

因此,如果输入是,比如说:
abca
,那么输出应该是0,如果输入是
abcb
,那么输出应该是1,否则应该是2。 我还想知道这个函数是否可以“堆叠”,比如我是否可以有多个空格和多个单词。

您可以使用和

#包括
#包括
#包括
#包括
int-lineFunction(标准::字符串行){
auto begin=std::find_if(std::begin(line),std::end(line),[](unsigned char c){return std::isspace(c);});
if(begin==std::end(line)| |++begin==std::end(line))返回2;
auto end=std::find_if(begin,std::end(line),[](unsigned char c){return std::isspace(c);});
std::字符串字(开始、结束);
如果(单词==“a”){
返回0;
}否则如果(单词==“b”){
返回1;
}否则{
返回2;
}
} 
int main(){
std::string line=“abc a”;

std::好吧,你不需要写出一个完整的函数,只要引导我在检测空白方面朝着正确的方向走就行了。你有没有做过任何研究?再加上?最方便的方法是将一行文本读入字符串,然后操纵字符串。否则,使用OS API读取击键并忽略空白。@AlgirdasPreidžius您如何使用这两种方法来检测后跟单词的空格?首先,您必须精确定义要执行的操作?例如,您可以在末尾修剪空格,检查长度是否至少为2,然后验证最后一个字符是否为空格,然后测试最后一个字符。如果您想要最后一个单词,这将起作用当它是单字母时。最好的做法是先画一个组织图或编写伪代码,然后实现它。您是否阅读了链接到的文档和示例?
//static\u cast(std::isspace)//错误
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

int lineFunction(std::string line) {
    auto begin = std::find_if(std::begin(line), std::end(line), [](unsigned char c){ return std::isspace(c); });
    if (begin == std::end(line) || ++begin == std::end(line)) return 2;
    auto end = std::find_if(begin, std::end(line), [](unsigned char c){ return std::isspace(c); });
    std::string word(begin, end);
    if (word == "a") {
        return 0;
    } else if (word == "b") {
        return 1;
    } else {
        return 2;
    }
} 

int main() {
    std::string line = "abc a";
    std::cout << lineFunction(line);
}