简体   繁体   English

奇怪的行为或std :: map :: const_iterator

[英]Strange behavior or std::map::const_iterator

I'm creating iterators: 我正在创建迭代器:

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();

my Report class looks: 我的报告类看起来如下:

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_;
};

Processed files in cpp looks like: cpp中的已处理文件如下所示:

typename Report::container_type_for_processed_files Report::processed_files()const
{
    return processed_files_;
}

but after initializing iterators as shown in the first lines: 但在初始化迭代器后,如第一行所示:

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;
            }

I'm getting error: Invalid parameter passed to C runtime function. 我收到错误消息:无效的参数传递给C运行时函数。
I'm also getting messages on output pane when trying to init iterators: 尝试初始化迭代器时,我还在输出窗格上收到消息:
(Internal error: pc 0x201 in read in psymtab, but not in symtab.) (内部错误:在psymtab中读取了pc 0x201,但在symtab中没有读取。)
What's going on? 这是怎么回事?

Without a compilable sample, I'm not sure this is your problem, but this looks fishy: 如果没有可编译的样本,我不确定这是您的问题,但这看起来很麻烦:

container_type_for_processed_files processed_files() const;

You should most likely be returning a const& to the container there, otherwise that function will be returning (potentially temporary) copies of the underlying container and your iterators will end up being invalid (not iterators to the same object, and possibly iterators to temporaries whose lifetime has ended). 您最有可能将const&返回到那里的容器,否则该函数将返回(可能是临时的)基础容器的副本,并且您的迭代器最终将变为无效(不是对同一对象的迭代器,并且可能对临时对象的迭代器无效)生命已经结束)。

Try with: 尝试:

container_type_for_processed_files const& processed_files() const;

One issue is that rep.processed_files() returns a copy of the underlying container. 一个问题是rep.processed_files()返回基础容器的副本 Therefore, beg and end apply to two separate objects; 因此, begend适用于两个单独的对象; and it's not meaningful to try to iterate from the beginning of one map to the end of another map - it will iterate past the end of the first container. 并且尝试从一个映射的开始到另一个映射的结束进行迭代是没有意义的-它会在第一个容器的末尾进行迭代。 You should return a reference to the contained object. 您应该返回对包含对象的引用。

typename Report::container_type_for_processed_files & Report::processed_files()const
{
    return processed_files_; // Returns by reference
}

And you need & when you define beg and 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();

There may also be other issues, as raised by others. 正如其他人提出的那样,可能还会有其他问题。 For example, what is qDebug ? 例如,什么是qDebug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM