Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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的调用没有匹配的函数(std::istream&;,int&;)_C++_String_Getline - Fatal编程技术网

C++ 对getline的调用没有匹配的函数(std::istream&;,int&;)

C++ 对getline的调用没有匹配的函数(std::istream&;,int&;),c++,string,getline,C++,String,Getline,我正在编写一个代码,将任务语句作为用户的输入,搜索其中是否存在“not”一词 如果不存在,那么它会打印真正的花式,否则它会定期打印花式,但我在这样一个简单的程序中遇到了一个错误 我的代码如下所示: #include <iostream> #include <string.h> using namespace std; int main () { string str2 (" not "); string str3 ("not "); string

我正在编写一个代码,将任务语句作为用户的输入,搜索其中是否存在“not”一词 如果不存在,那么它会打印真正的花式,否则它会定期打印花式,但我在这样一个简单的程序中遇到了一个错误

我的代码如下所示:

#include <iostream>
#include <string.h>
using namespace std;

int main () {
    string str2 (" not ");
    string str3 ("not ");
    string str4 ("not");

    int T;
    std::getline(std::cin, T);
    for (int k=0;k<T;k++){
        string str ;


        std::getline(std::cin, str);
        int len = str.size();
        //condition for only not as a sentence
        if ((str.find(str4) != string::npos) && len ==3) {
            cout<<"Real Fancy";
        }        
        // condition to check for not word in middle of a sentence eg. this is not good
        else if ((str.find(str2) != string::npos) ) {
            cout<<"Real Fancy";
        }        
        // condition if the statement ends with the word not 
        else if (str[len-1]=='t' && str[len-2]== 'o' && str[len-3]== 'n' && str[len-4]== ' '){
            cout<<"Real Fancy";
        }        
        // code to check for if statement starts with word not
        else if ((str.find(str3) != string::npos) ) {
            cout<<"Real Fancy";
        }
        else {
            cout<<"regularly fancy";
        }
        cout<<endl;
    }
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串str2(“非”);
字符串str3(“非”);
字符串str4(“非”);
int T;
标准::getline(标准::cin,T);
对于(int k=0;k根据

您应该希望在以下表单中使用getline:

istream& getline (istream&  is, string& str); 
istream& getline (istream&& is, string& str);
您可以这样做:

int T;
std::getline(std::cin, T);
T
是一个整数,不能进行此类调用。

有两个重载:

(1)istream&getline(istream&is,string&str,char-delim);
(2) istream和getline(istream和is、string和str);
您试图使用
int
参数调用它,但该参数无效

有两种方法可以解决此问题:

用于在读入
std::字符串后解析
int

使用<代码> STD::CIN > t>代码>直接从流读取到<代码> INT/>。这将不考虑新的行,并且使用任何空白作为INTS之间的分隔符。因此,如果您试图解析每行中的一个<代码> INT/COD>,前一个选项将更适合您。<> >代码> STD::GETLINE()

函数不适用于整数类型。 如果要对整数使用
std::getline()
,则应声明临时字符串类型值。 比如你的情况

int T;
std::string tmp_s;
std::getline(std::cin, tmp_s);
T = std::stoi(tmp_s);
具有以下重载:

istream&getline(istream&is,string&str,char-delim);
istream和getline(istream和is、string和str、char-delim);
istream和getline(istream和is、string和str);
istream&getline(istream&is,string&str);
如您所见,它们中没有一个将integer作为第二个参数接收,因此以下是可能的解决方案:

  • 如果您仅限于C++11,请按以下方式使用:
std::字符串行;
std::getline(std::cin,line);
int T=std::stoi(直线);
  • 如果您能够使用C++17,我强烈建议您使用以下方法:
int值=0;
std::字符串行;
std::getline(std::cin,line);
const auto result=std::from_chars(str.data(),str.data()+str.size(),value);
如果(result.ec==std::errc()){
//字符串已成功解析
}
为什么
std::from_chars
优于
std::stoi


因为
std::from_chars
是非抛出的,它提供了更好的错误报告(这将为您提供有关转换结果的更多信息)另外,它也比<代码> STD::STOI 根据一些资源。请阅读更多关于<代码> STD::FasuxCARS < /C> >

为什么<代码> STD::CIN > T/<代码>不考虑行限制?新行也是空白区。可能只是一个措辞的问题。我的意思是它使用任何空白作为分隔符,包括换行符。ks-like OP希望每行专门解析一个int,而流运算符不考虑这一点。您可以轻松地改进您的问题:只需删除整个
for
循环。没有它,您将得到相同的编译器错误消息。请记住,最好使用最小的工作示例。对于您的所有贡献,thanx非常有用我犯了错误。我将立即尝试提供解决方案,并尽快回击