Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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++ string::find在';s应返回值0[C+;+;]_C++ - Fatal编程技术网

C++ string::find在';s应返回值0[C+;+;]

C++ string::find在';s应返回值0[C+;+;],c++,C++,我有以下不起作用的代码: string line; string line_sub; size_t open_tag_start; const string open_tag = "<image>"; const int open_len = open_tag.length() + 1; open_tag_start = line.find(open_tag); line_sub = line.substr(open_tag_start, open_len); 与 我的问题消失了

我有以下不起作用的代码:

string line;
string line_sub;
size_t open_tag_start;
const string open_tag = "<image>";
const int open_len = open_tag.length() + 1;

open_tag_start = line.find(open_tag);
line_sub = line.substr(open_tag_start, open_len);

我的问题消失了,我可以编译和执行代码

这是我的程序的一个简短版本,只包含引起问题的部分。试图编译此代码将产生上面详述的错误消息。文件
rss.xml
是engadget.com的rss提要

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
无效获取标签内容(ifstream&rssfile、字符串行、字符串打开标签);
int main()
{
const string open_tag=“”;
ifstream-rssfile;
rssfile.open(“rss.xml”);
弦线;
getline(rssfile,行'\n');
获取标签内容(rssfile、line、open标签);
返回0;
}
无效获取标记内容(ifstream&rssfile、字符串行、字符串打开标记)
{   
const int open_len=open_tag.length()+1;
大小\u t打开\u标记\u开始;
弦线_-sub;
open_tag_start=line.find(open_tag);
line\u sub=line.substr(打开标签开始,打开透镜);
}

除非您遗漏了一些代码,
是一个空字符串,所以当然
查找
会失败。出错的是您的期望,而不是
find
函数


作为一个旁注,在使用C++字符串时,不需要对<代码> 0”<代码>进行补偿。除去

+1

除非您遗漏了一些代码,
是一个空字符串,因此当然
查找
会失败。出错的是您的期望,而不是
find
函数


作为一个旁注,在使用C++字符串时,不需要对<代码> 0”<代码>进行补偿。去掉

+1

如果在字符串中找不到子字符串,则
find()
方法将返回
std::string::npos
,这是一个大小类型的值-1。当您使用
open\u tag\u start
equalling-1调用
substr()
时,这就是抛出范围外错误的原因。

如果在字符串中找不到子字符串,则
find()
方法将返回
std::string::npos
,这是值-1的大小类型。当您使用
open\u tag\u start
equalling-1调用
substr()
时,这就是抛出范围外错误的原因。

那么,您确定
包含
??需要考虑的是,<代码> <代码>可能以某种方式大写。另外,我不太明白您想做什么,因为
line\u sub
(假设您的代码正常工作)将返回
enter code here
,然后返回下一个字符。你想完成什么


记住清理返回值。如果find返回-1,则处理问题或抛出错误。

那么,您确定
包含
??需要考虑的是,<代码> <代码>可能以某种方式大写。另外,我不太明白您想做什么,因为
line\u sub
(假设您的代码正常工作)将返回
enter code here
,然后返回下一个字符。你想完成什么


记住清理返回值。如果find返回-1,请处理问题或抛出错误。

正如其他人所指出的,如果搜索失败,您必须检查find()的返回值

std::string line("this is <image> a test");
std::string line_sub;
const std::string open_tag = "<image>";

size_t open_tag_start = line.find(open_tag);

if (open_tag_start != std::string::npos)
{
    line_sub = line.substr(open_tag_start, open_tag.length());
}

std::cout << line << "\n" << line_sub << "\n";
std::字符串行(“这是一个测试”);
std::字符串行_sub;
const std::string open_tag=“”;
size\u t open\u tag\u start=line.find(open\u tag);
如果(打开标签开始!=std::string::npos)
{
line_sub=line.substr(open_tag_start,open_tag.length());
}

std::cout正如其他人所指出的,如果搜索失败,您必须检查find()的返回值

std::string line("this is <image> a test");
std::string line_sub;
const std::string open_tag = "<image>";

size_t open_tag_start = line.find(open_tag);

if (open_tag_start != std::string::npos)
{
    line_sub = line.substr(open_tag_start, open_tag.length());
}

std::cout << line << "\n" << line_sub << "\n";
std::字符串行(“这是一个测试”);
std::字符串行_sub;
const std::string open_tag=“”;
size\u t open\u tag\u start=line.find(open\u tag);
如果(打开标签开始!=std::string::npos)
{
line_sub=line.substr(open_tag_start,open_tag.length());
}

std::cout…由于size\u t是无符号的,所以它没有返回
-1
,而是返回
string::npos
@bgporter——尽管如此,在标准中string::npos的值被定义为-1。@PigBen:从技术上讲,它不是也不能被定义为-1的值,因为它超出了可表示为
size\u type
的值范围。它被定义为
size\u类型
,初始值设定为
-1
,因此在转换为
size\u类型
后,它的值为
-1
,这是一个很大的正数。我的问题用词不对,我提供了一个更新,使事情更清楚一些……而且由于size\u类型是无符号的,它没有返回
-1
,它返回
string::npos
@bgporter——尽管如此,在标准中,string::npos的值被定义为-1。@PigBen:从技术上讲,它没有也不能被定义为-1,因为它超出了
size\u类型
可表示的值范围。它被定义为
size\u类型
,初始值设定为
-1
,因此在转换为
size\u类型
后,它的值为
-1
,这是一个很大的正数。我的问题措辞错误,我提供了一个更新,让事情变得更清楚。技术上,
std::string::size\u类型
不能接受值
-1
,因为它是无符号类型
std::string::npos
是可表示为
size\u type
的最大数字,它与
-1
转换为
size\u type
相同,但不是
-1
。我的问题措辞错误,我提供了一个更新,让事情变得更清楚。技术上,
std::string::size\u类型
不能接受值
-1
,因为它是无符号类型
std::string::npos
是可表示为
siz的最大数字
line_sub = line.substr(0, open_len);
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
using namespace std;

void get_tag_contents(ifstream& rssfile, string line, string open_tag);

int main()
{
    const string open_tag = "<image>";

    ifstream rssfile;
    rssfile.open("rss.xml");
    string line;

    getline(rssfile, line, '\n');
    get_tag_contents(rssfile, line, open_tag);

    return 0;
}

void get_tag_contents(ifstream& rssfile, string line, string open_tag)
{   
    const int open_len = open_tag.length() + 1;
    size_t open_tag_start;
    string line_sub;

    open_tag_start = line.find(open_tag);
    line_sub = line.substr(open_tag_start, open_len);
}
std::string line("this is <image> a test");
std::string line_sub;
const std::string open_tag = "<image>";

size_t open_tag_start = line.find(open_tag);

if (open_tag_start != std::string::npos)
{
    line_sub = line.substr(open_tag_start, open_tag.length());
}

std::cout << line << "\n" << line_sub << "\n";