std :::: map<>

发布于 2025-01-29 07:09:08 字数 366 浏览 1 评论 0原文

假设我有一个std :: Map使用

a[1] = "One";
a[2] = "Two";
a[3] = "Three";

,此映射表示用户在列表控件中添加的字符串。

现在,用户决定将项目1拖到列表控件的底部。

是否有一种简单的方法可以重新装修地图,以便它是:

a[1] = "Two";
a[2] = "Three";
a[3] = "One";

或者我选择了错误的数据结构来保存数据?

PS:我不能使用std :: vector,因为它不是分类的容器,我需要按照该顺序处理字符串。

Lets say I have a std::map with

a[1] = "One";
a[2] = "Two";
a[3] = "Three";

and this map is represent strings that user added in the list control.

Now, the user decides to drag the item 1 at the bottom of the list control.

Is there an easy way to reshuffle the map so that it is:

a[1] = "Two";
a[2] = "Three";
a[3] = "One";

Or maybe I chose a wrong data structure to hold data?

P.S.: I can't use std::vector, because it's not a sorted container and I need to process string in that order.

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

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

发布评论

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

评论(1

小巷里的女流氓 2025-02-05 07:09:08

我知道您的帖子声明是您将无法使用std :: vector存储元素,因为它不是“不是排序的容器”,如Shadowranger在他的评论中指出的那样,如果您使用的话Integer键表明,使用std :: Map使用std :: vector使用std :: map实现类似的结构。将矢量的索引作为键。这样一来,如果您希望按升级的关键顺序迭代向量,则可以按顺序迭代矢量。您甚至可以使用用于地图的int键访问元素,只需从0而不是1开始。

对于移动元素,您可以使用类似的代码

template <typename Iter>
void shiftBack(Iter begin, Iter end){
    for(;end > begin; --end){
        *end = std::move(*begin);
    }
}

template <typename Iter>
void shiftFront(Iter begin, Iter end){
    for(;begin < end; ++begin){
        *(begin - 1) = std::move(*begin);
    }
}

template <typename Iter>
void moveElem(Iter elemPos, Iter newPos){
    if(elemPos == newPos){
        return;
    }

    auto temp = std::move(*elemPos);

    if(elemPos > newPos){
        shiftBack(newPos, elemPos);
    } else {
        shiftFront(elemPos + 1, newPos + 1);
    }

    *newPos = std::move(temp);
}

此代码只是一个示例这是关于要做什么的一般想法,我不承诺它会在没有错误的情况下起作用。

I know your post claim's you would be unable to use a std::vector to store the elements as it is "not a sorted container" however, as ShadowRanger pointed out in his comment, if your use of integer keys indicates that it would be more than possible to implement a similar structure to what you are doing with std::map with std::vector. Using the indices of the vector as your keys. That way if you wished to iterate over the vector in ascending key order, you could simply iterate over the vector in order. You could even access the elements using the int keys you used for your map, just starting with 0 instead of 1.

As for moving the elements, you could use some code like this

template <typename Iter>
void shiftBack(Iter begin, Iter end){
    for(;end > begin; --end){
        *end = std::move(*begin);
    }
}

template <typename Iter>
void shiftFront(Iter begin, Iter end){
    for(;begin < end; ++begin){
        *(begin - 1) = std::move(*begin);
    }
}

template <typename Iter>
void moveElem(Iter elemPos, Iter newPos){
    if(elemPos == newPos){
        return;
    }

    auto temp = std::move(*elemPos);

    if(elemPos > newPos){
        shiftBack(newPos, elemPos);
    } else {
        shiftFront(elemPos + 1, newPos + 1);
    }

    *newPos = std::move(temp);
}

This code is just an example that follows the general idea for what to do, I make no promises that it will work without error.

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