为什么我不能在地图中放置迭代器?

发布于 2024-07-12 12:00:12 字数 626 浏览 2 评论 0原文

我有一个这样定义的地图

std::map<some_key_type, std::string::iterator> mIteratorMap;

和一个名为“mHugeString”的巨大字符串。 然后我像这样遍历收集迭代器的字符串:

std::string::iterator It=mHugeString.begin();
std::string::iterator EndIt=mHugeString.end();
for(;It!=EndIt;++It){
  ...defining a key element...
  if(need_to_store_an_iterator)mIteratorMap[key_of_a_right_type]=It;
}

最后我应该收到一个映射,其中迭代器与某种键相关联。 但是,当通过“make_pair”与键配对时,迭代器会以某种方式失去自身,除非它指向字符串末尾的某个位置。 很难说,但也许最后 256 个字节就可以了。

所以问题不在于如何避免丢失迭代器,无论如何存储它们都是一个愚蠢的想法,但为什么尝试在字符串的开头存储迭代器会失败,以及为什么在末尾的迭代器同样可以正常工作? 它们之间有什么区别?

I have a map defined like this

std::map<some_key_type, std::string::iterator> mIteratorMap;

And a huge string named "mHugeString". Then I walk trough the string collecting iterators like this:

std::string::iterator It=mHugeString.begin();
std::string::iterator EndIt=mHugeString.end();
for(;It!=EndIt;++It){
  ...defining a key element...
  if(need_to_store_an_iterator)mIteratorMap[key_of_a_right_type]=It;
}

In the end I should recieve a map, where an iterator is associated with a some sort of key. But the iterator somehow looses itself when being paired with a key by "make_pair", unless it points to a place somewhere in the end of a string. It's hard to tell, but maybe last 256 bytes are fine.

So the question is not how to avoid loosing iterators, it was a stupid idea to store them anyways, but why trying to store an iterator in the begining of the string fails, and why the same with the iterators on the end works fine? What is the difference between them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷你仙 2024-07-19 12:00:12

我还没有尝试过,但我预计,您当然可以将迭代器值存储为映射中的值。

您是否知道,如果更改 mHugeString 的内容,那么您之前存储的任何迭代器现在都无效?

您可以选择将索引存储到字符串中,而不是迭代器中。

I haven't tried it but I would have expected that, of course you can store iterator values as values in a map.

Do you know that if you change the contents of mHugeString then any iterators into it which you have previously stored are now invalid?

You might choose to store the index into the string, instead of the iterator.

暖伴 2024-07-19 12:00:12

我不知道为什么它会成为问题。 我编写此代码是为了检查迭代器的存储和检索,这似乎工作正常。 [注意:我没有使用 make_pair,因为它不应该相关,但请尝试不使用它!]

#include <string>
#include <iostream>
#include <map>

enum UniqueKey {One, Two, Three};

typedef std::map<UniqueKey, std::string::iterator> UniqueKeyStringMap;

int main()
{
    UniqueKeyStringMap storage;

    std::string stringOne = "This is one string";

    std::string::iterator it = stringOne.begin() + 8; // "o"
    std::cout << " Iterator to store: " << std::string(it, it + 3) << std::endl;

    storage[UniqueKey::One] = it;   // Store iterator

    // Retrieve and print, string and map are valid

    UniqueKeyStringMap::iterator mapIter = storage.find(UniqueKey::One);
    if (mapIter != storage.end())
    {
        std::cout << " From storage: " << 
            std::string(mapIter->second, mapIter->second + 3) << std::endl;
    }
}

预期输出:
用于存储的迭代器:1
来自存储: 1

I am not sure why it should be problem. I wrote this code to check storage and retrieval of iterator which seems to work fine. [Note: I am not using make_pair as it should not be relevant, give try without using it though!]

#include <string>
#include <iostream>
#include <map>

enum UniqueKey {One, Two, Three};

typedef std::map<UniqueKey, std::string::iterator> UniqueKeyStringMap;

int main()
{
    UniqueKeyStringMap storage;

    std::string stringOne = "This is one string";

    std::string::iterator it = stringOne.begin() + 8; // "o"
    std::cout << " Iterator to store: " << std::string(it, it + 3) << std::endl;

    storage[UniqueKey::One] = it;   // Store iterator

    // Retrieve and print, string and map are valid

    UniqueKeyStringMap::iterator mapIter = storage.find(UniqueKey::One);
    if (mapIter != storage.end())
    {
        std::cout << " From storage: " << 
            std::string(mapIter->second, mapIter->second + 3) << std::endl;
    }
}

expected output:
Iterator to store: one
From storage: one

惜醉颜 2024-07-19 12:00:12

没关系,您可以在映射中存储迭代器。 如果您遇到一些错误,那是由其他原因引起的。 请注意,如果修改字符串,则指向字符串的迭代器将变得无效。

请向我们展示一个完整的、可编译的但无法使用的代码片段,以便我们对其进行分析。

It's fine, you can store iterators in the map. If you get some error, that is caused by something else. Note that if you modify your string, iterators pointing into your string will become invalid.

Please show us a complete, compilable code snippet that is rendered unusable, so we can analyze it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文