Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++ 奇怪的行为或std::map::const_迭代器_C++_Map_Iterator - Fatal编程技术网

C++ 奇怪的行为或std::map::const_迭代器

C++ 奇怪的行为或std::map::const_迭代器,c++,map,iterator,C++,Map,Iterator,我正在创建迭代器: typename Report::container_type_for_processed_files::const_iterator beg = rep.processed_files().begin(); typename Report::container_type_for_processed_files::const_iterator end = rep.processed_files().end(); 我的报告课看起来: class Rep

我正在创建迭代器:

typename Report::container_type_for_processed_files::const_iterator beg = rep.processed_files().begin();
            typename Report::container_type_for_processed_files::const_iterator end = rep.processed_files().end();
我的报告课看起来:

class Report
{
public:
typedef std::map<boost::filesystem3::path,
                 std::pair<unsigned long long/*code*/,
                           unsigned long long/*comment*/> > container_type_for_processed_files;
container_type_for_processed_files processed_files()const;
private:
container_type_for_processed_files processed_files_;
};
但是在初始化迭代器之后,如前几行所示:

typename Report::container_type_for_processed_files::const_iterator beg = rep.processed_files().begin();
            typename Report::container_type_for_processed_files::const_iterator end = rep.processed_files().end();
            while (beg != end)
            {
                qDebug() << beg->first.c_str();//here I'm getting runtime error
                fout << "File name: " << (beg->first).c_str();


                ++beg;
            }
typename报告::容器\类型\处理的\文件::常量\迭代器beg=rep.processed\文件().begin();
typename报告::容器\u类型\u用于已处理的\u文件::常量\u迭代器end=rep.processed\u文件().end();
while(beg!=结束)
{
qDebug()first.c_str();//这里是运行时错误

fout一个问题是
rep.processed_文件()
返回基础容器的副本。因此,
beg
end
应用于两个单独的对象;尝试从一个映射的开头迭代到另一个映射的结尾没有意义-它将迭代到第一个容器的结尾。您应该返回对所包含对象的引用

typename Report::container_type_for_processed_files & Report::processed_files()const
{
    return processed_files_; // Returns by reference
}
定义beg和end时,需要
&

typename Report::container_type_for_processed_files::const_iterator & beg = rep.processed_files().begin();
typename Report::container_type_for_processed_files::const_iterator & end = rep.processed_files().end();

也可能有其他人提出的其他问题。例如,什么是
qDebug

一个问题是
rep.processed_files()
返回基础容器的副本。因此,
beg
end
应用于两个单独的对象;尝试从一个映射的开头迭代到另一个映射的结尾没有意义-它将迭代到第一个容器的结尾。您应该返回对所包含对象的引用

typename Report::container_type_for_processed_files & Report::processed_files()const
{
    return processed_files_; // Returns by reference
}
定义beg和end时,需要
&

typename Report::container_type_for_processed_files::const_iterator & beg = rep.processed_files().begin();
typename Report::container_type_for_processed_files::const_iterator & end = rep.processed_files().end();

可能还有其他人提出的问题。例如,如果没有可编译的示例,什么是
qDebug

,我不确定这是您的问题,但这看起来很可疑:

container_type_for_processed_files processed_files() const;
您最有可能将
常量&
返回到容器中,否则该函数将返回底层容器的副本(可能是临时的),您的迭代器将最终无效(不是对同一对象的迭代器,也可能是对生存期已结束的临时容器的迭代器)

尝试:

container_type_for_processed_files const& processed_files() const;

如果没有可编译的示例,我不确定这是否是您的问题,但这看起来很可疑:

container_type_for_processed_files processed_files() const;
您最有可能将
常量&
返回到容器中,否则该函数将返回底层容器的副本(可能是临时的),您的迭代器将最终无效(不是对同一对象的迭代器,也可能是对生存期已结束的临时容器的迭代器)

尝试:

container_type_for_processed_files const& processed_files() const;

另外,请考虑创建一个最小的测试用例(参见).@ OLICHARLSVESY,一个QT提供的输出流,用于调试。也请考虑创建一个最小的测试用例(参见)。@ OLICHARLSVESY是一个QT提供的输出流,用于调试目的。@ Simulb:您忘记给出<代码> + 1 < /代码>:D.@ Simulb:您忘记给出<代码> + 1 < /C> >:D。