Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.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++ 交换地图的两个元素_C++ - Fatal编程技术网

C++ 交换地图的两个元素

C++ 交换地图的两个元素,c++,C++,我有一个map其中按钮类有几个属性,特别是一个名为position的整数变量 如果我想在Button类中交换两个位置,我必须更改键,使其始终为key=Button->position,并且它必须是一张地图 我想删除地图的两个位置(使用擦除)并重新插入(指示索引): 示例(已知indexFirst和indexSecond): 地图按钮; int posOfFirst=buttons.find(indexFirst)->second->getPos(); int posOfSecond=button

我有一个
map
其中按钮类有几个属性,特别是一个名为position的整数变量

如果我想在Button类中交换两个位置,我必须更改键,使其始终为key=Button->position,并且它必须是一张地图

我想删除地图的两个位置(使用擦除)并重新插入(指示索引):

示例(已知indexFirst和indexSecond):

地图按钮;
int posOfFirst=buttons.find(indexFirst)->second->getPos();
int posOfSecond=buttons.find(indexSecond)->second->getPos();
Button*button1=按钮。查找(indexFirst)->秒;
Button*button2=按钮。查找(索引秒)->秒;
按钮。擦除(indexFirst);
按钮。擦除(indexFirst);
按钮[posOfSecond]=按钮2;
按钮[posOfFirst]=按钮1;

但似乎不会更改对象。为什么?

您正在擦除同一元素(在indexFirst)两次(查看您的代码)。此外,您似乎正在将图元插入其初始位置:

buttons[posOfSecond] = button2;
buttons[posOfFirst] = button1;
我认为这件事应该改为:

buttons[pos1] = button2;
buttons[pos2] = button1;
我也会推荐一个更好的策略。在Button类中创建一个mutator方法,允许您设置position属性的值,而不是处理删除和插入。然后,您只需获取两个按钮的位置(就像在代码的第一部分中使用访问器方法所做的那样),然后将第一个位置分配给第二个按钮,将第二个位置分配给第一个按钮。您的按钮标题中应该有如下内容:

void setPos(int pos);
下面是一个例子:

map<int, Button*> buttons;

//Find the buttons only once and save their references 
//if you need further information that 
//their storing, instead of constantly searching 
//through the map. This is more efficient
Button* button1 = buttons.find(indexFirst)->second;
Button* button2 = buttons.find(indexSecond)->second;

int pos1 = button1->getPos();
int pos2 = button2->getPos();

button1->setPos(pos2);
button2->setPos(pos1);

buttons[pos2] = button1;
buttons[pos1] = button2;
地图按钮;
//只查找按钮一次并保存其引用
//如果您需要进一步的信息
//它们的存储,而不是不断地搜索
//通过地图。这更有效
Button*button1=按钮。查找(indexFirst)->秒;
Button*button2=按钮。查找(索引秒)->秒;
int pos1=按钮1->getPos();
int pos2=按钮2->getPos();
按钮1->设置位置(位置2);
按钮2->设置位置(位置1);
按钮[pos2]=按钮1;
按钮[pos1]=按钮2;
你就完了

如果按钮存储的唯一唯一数据是它们的位置,则会出现这种情况,否则您也必须交换其他信息


这里有很多策略,有不同的交易of,但请确保您始终不仅考虑它是否有效,而且考虑它是否有效。

但您在哪里进行交换呢?看看你的代码,我没看到。Button1位于位置indexFirst或posOfFirst,button2位于位置indexSecond或posOfSecond,这在您的代码中没有更改。如果
getPos()
返回的值与用于将其存储在地图中的键值不同,则这将不起作用。@CaptainObvlious是的,我看到代码有问题,但是您没有正确指定,我将清理此答案的注释,这样就不会出现混乱。
map<int, Button*> buttons;

//Find the buttons only once and save their references 
//if you need further information that 
//their storing, instead of constantly searching 
//through the map. This is more efficient
Button* button1 = buttons.find(indexFirst)->second;
Button* button2 = buttons.find(indexSecond)->second;

int pos1 = button1->getPos();
int pos2 = button2->getPos();

button1->setPos(pos2);
button2->setPos(pos1);

buttons[pos2] = button1;
buttons[pos1] = button2;