C++ 在multiset中维护数字的排序列表,并在另一个集合中维护累积和
我有一个应用程序,其中整数没有特定的顺序。显示的整数可以是重复值。我必须以分类的方式维护它们。每次出现新条目时,都需要将其放置在适当的位置,以便保持排序顺序 似乎是一个建议的解决方案,插入的最佳时间是C++ 在multiset中维护数字的排序列表,并在另一个集合中维护累积和,c++,arrays,sorting,containers,multiset,C++,Arrays,Sorting,Containers,Multiset,我有一个应用程序,其中整数没有特定的顺序。显示的整数可以是重复值。我必须以分类的方式维护它们。每次出现新条目时,都需要将其放置在适当的位置,以便保持排序顺序 似乎是一个建议的解决方案,插入的最佳时间是O(logn) 现在,除了这个排序的多集,我还必须在另一个容器中维护累积和 也就是说,如果排序的条目是: 1,5,7,9(在指数0,1,2和3中) 累计总和容器应为: 1、6、13、22(在指数0、1、2和3中) 我很难弄清楚如何使用std::multiset迭代器,该迭代器在每次insert(in
O(logn)
现在,除了这个排序的多集,我还必须在另一个容器中维护累积和
也就是说,如果排序的条目是:
1,5,7,9(在指数0,1,2和3中)
累计总和容器应为:
1、6、13、22(在指数0、1、2和3中)
我很难弄清楚如何使用std::multiset
迭代器,该迭代器在每次insert(int)
操作后返回到multiset中,以便更新累积和容器。请注意,累积和只会影响因插入操作而必须移动的条目和索引
也就是说,如果必须执行上述列表中的插入(8),则更新的容器将是:
已排序的条目:
1,5,7,8,9(在指数0,1,2,3和4中)
累计金额:
1、6、13、21、30(在指数0、1、2、3和4中。请注意,只有指数3和4中的条目受影响。)
目前,我能够实现这一点的唯一方法是使用两个数组,一个用于值数组,另一个用于累积和。实现此功能的工作代码如下所示:
#include <iostream>
int *arr = new int[100];//Array to maintain sorted list
int *cum_arr = new int[100];//Array to maintain cumulative sum
void insert_into_position(int val, int &last_valid_index_after_insertion) {
//Inserts val into arr such that after insertion
//arr[] has entries in ascending order.
int postoadd = last_valid_index_after_insertion;
//index in array at which to insert val
//initially set to last_valid_index_after_insertion
//Search from end of array until you find the right
//position at which to insert val
for (int ind = last_valid_index_after_insertion - 1; ind >= 0; ind--) {
if (arr[ind] > val) {
postoadd--;
}
else {
break;
}
}
//Move everything from and including postoadd one position to the right.
//Update the cumulative sum array as you go
for (int ind = last_valid_index_after_insertion - 1; ind >= postoadd; ind--) {
arr[ind + 1] = arr[ind];
cum_arr[ind + 1] = cum_arr[ind] + val;
}
//Update entry in index postoadd
arr[postoadd] = val;
if (postoadd > 0)
cum_arr[postoadd] = cum_arr[postoadd - 1] + val;
else
cum_arr[0] = val;
last_valid_index_after_insertion++;
}
int main(void)
{
int length = 0;
insert_into_position(1, length);
insert_into_position(5, length);
insert_into_position(7, length);
insert_into_position(9, length);
printf("\nPrint sorted array\n");
for (int i = 0; i < length; i++)
printf("%d ", arr[i]);
printf("\nPrint Cumulative sum array\n");
for (int i = 0; i < length; i++)
printf("%d ", cum_arr[i]);
insert_into_position(8, length);
printf("\nPrint sorted array\n");
for (int i = 0; i < length; i++)
printf("%d ", arr[i]);
printf("\nPrint Cumulative sum array\n");
for (int i = 0; i < length; i++)
printf("%d ", cum_arr[i]);
getchar();
}
#包括
int*arr=新int[100]//数组来维护排序列表
int*cum_arr=新int[100]//数组来维护累积和
无效插入位置(插入后的int val、int和最后一个有效索引){
//将val插入arr,以便在插入后
//arr[]具有按升序排列的条目。
int postoadd=插入后最后一个有效索引;
//数组中要插入val的索引
//初始设置为插入后最后一个有效索引
//从数组末尾搜索,直到找到正确的
//插入val的位置
for(int ind=插入后最后一个有效的索引-1;ind>=0;ind--){
如果(arr[ind]>val){
加载后--;
}
否则{
打破
}
}
//从一个位置向右移动所有内容,包括加载后的内容。
//在运行时更新累积和数组
for(int ind=插入后最后一个有效索引-1;ind>=后加载;ind--){
arr[ind+1]=arr[ind];
cum_arr[ind+1]=cum_arr[ind]+val;
}
//在索引加载后更新条目
arr[postoadd]=val;
如果(加载后>0)
cum_arr[postoadd]=cum_arr[postoadd-1]+val;
其他的
cum_arr[0]=val;
插入++后的最后一个有效索引;
}
内部主(空)
{
整数长度=0;
将_插入_位置(1,长度);
将_插入_位置(5,长度);
将_插入_位置(7,长度);
将_插入_位置(9,长度);
printf(“\n打印排序数组\n”);
for(int i=0;i
从这段代码中可以看出,为了计算累积和,可以使用整数数组索引postoadd
,直到到达数组的末尾
是否有任何容器组合可以比两个整数数组执行得更好/更高效
std::multiset.insert(int)
操作的返回类型是指向插入项的迭代器。是否可以使用此迭代器来更新另一个存储累积和的容器?使用一个,它可以对键进行排序,并允许重复键
例如:
#include <iostream>
#include <map>
int main ()
{
std::multimap<int,int> mymultimap = { {1, 1}, {5, 6}, {7, 13}, {9, 22} };
std::multimap<int,int>::iterator it;
it = mymultimap.insert (std::pair<char,int>(8, 8));
if(mymultimap.size() > 1) {
it->second = std::prev(it)->second + it->second;
++it;
while(it!=mymultimap.end()) {
it->second = std::prev(it)->second + it->first;
++it;
}
}
// showing contents:
std::cout << "mymultimap contains:\n";
for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
std::cout << (*it).first << " => " << (*it).second << '\n';
return 0;
}
PS:另一种方法是使用
std::multiset
,其中每个元素都是std::pair
,其中第一个是数字,第二个是累积和。因此允许重复值,例如1、5、7、9、9
和1、6、13、22、31
,对吗?不要维护两个单独的多集,而是使用一对多集。每对中的第一个是值,第二个是累积和。insert返回的迭代器将用于-取上一个值的第二个值并计算插入项的累计和,然后从iter+1循环到end(),以更新以下所有值中的第二个值entries@gsamaras是的,重复的是allowed@ArtemyVysotsky我认为这确实可行。是否有方法插入并指示multiset根据对中的第一个进行排序?@Tryer是的,这是一个MultiMap。在++it的while(it!=mymultimap.end()){}内的操作,每个增量都是常量时间吗?也就是说,下次需要它->第一次或它->第二次时,访问是否立即进行?感谢您提供完整的工作代码。单独-当insert返回的是begin()时,应处理大小写。其上的prev()将导致未定义behavior@Tryer是的,恒定时间。这取决于它们是否存在于数据缓存中(如果这是您要问的)。@ArtemyVysotsky谢谢,代码已更新!谢谢你的投票@gsamaras您当前的解决方案似乎不正确。例如,如果我插入(-1,-1),返回的迭代器将是开始,您的代码将不会做任何进一步的事情。而实际上它应该做一些更新。
mymultimap contains:
1 => 1
5 => 6
7 => 13
8 => 21
9 => 30