C++ 汇总矢量数据C++;
我在向量上实现函数时遇到问题。此函数的目标是通过返回值及其频率的向量来汇总向量中的数据。我有下面的实现,但它似乎没有为新向量存储正确的值。有什么建议吗?谢谢大家!C++ 汇总矢量数据C++;,c++,vector,C++,Vector,我在向量上实现函数时遇到问题。此函数的目标是通过返回值及其频率的向量来汇总向量中的数据。我有下面的实现,但它似乎没有为新向量存储正确的值。有什么建议吗?谢谢大家! //REQUIRES: v is not empty //EFFECTS: returns a summary of the dataset as (value, frequency) pairs // In the returned vector-of-vectors, the inner vector is a (value,
//REQUIRES: v is not empty
//EFFECTS: returns a summary of the dataset as (value, frequency) pairs
// In the returned vector-of-vectors, the inner vector is a (value, frequency)
// pair. The outer vector contains many of these pairs. The pairs should be
// sorted by value.
// {
// {1, 2},
// {2, 3},
// {17, 1}
// }
//
// This means that the value 1 occurred twice, the value 2 occurred 3 times,
// and the value 17 occurred once
std::vector<std::vector<double> > summarize(std::vector<double> v);
//需要:v不是空的
//效果:以(值、频率)对的形式返回数据集的摘要
//在向量的返回向量中,内部向量是(值、频率)
//配对。外向量包含许多这样的对。这对应该是
//按值排序。
// {
// {1, 2},
// {2, 3},
// {17, 1}
// }
//
//这意味着值1出现两次,值2出现三次,
//值17出现一次
标准::向量汇总(标准::向量v);
向量汇总(向量v){
病媒概述;
排序(v);
尺寸=v.尺寸();
如果(v.empty()==false){
int计数器=1;
对于(大小i=0;i
如果(v[i]!=v[i-1]),您的代码中有一个错误。在我为零时,您引用了i-1元素
我可以举两个解决方案为例,但它们都依赖于解决方案中使用的std::sort算法的结果
第一种是基于索引的,即您尝试实现它的方式。它以向量的向量形式返回结果。它是模板化的,不仅可以接受双精度。如果您不需要它,只需删除所有模板人员并用您需要的类型替换t
当心操作员=代码>-由于舍入误差的影响,比较双精度通常不是一个好主意。但是在你的例子中,如果你先初始化两倍,然后立即比较它们,而不需要中间数学,这可能是可以的
另外,更好的方法可能是返回std::pair,其中std::size\u t代表计数器。第二个解决方案实现了这一点,它还显示了迭代器的使用
#include <utility>
#include <vector>
#include <algorithm>
// Index based solution
template<typename T>
std::vector<std::vector<double>> summarize(std::vector<T> v) {
if (v.empty()) return {};
std::vector<std::vector<double> > summary;
std::sort(v.begin(), v.end());
// first element
T currentValue = *v.begin();
summary.emplace_back(std::vector{currentValue, static_cast<T>(1)});
// parse the remaining elements starting with index 1
for (std::size_t i = 1; i < v.size(); ++i) {
if (currentValue != v.at(i)) {
currentValue = v.at(i);
summary.emplace_back(std::vector{currentValue, static_cast<T>(1)});
} else {
++summary.back().at(1);
}
}
return summary;
}
// Iterator and std::pair based solution
template<typename T>
std::vector<std::pair<T,std::size_t>> summarizeIt(std::vector<T> v) {
if (v.empty()) return {};
std::vector<std::pair<T,std::size_t>> summary;
std::sort(v.begin(), v.end());
summary.emplace_back(*v.begin(), 1);
for (auto it = v.begin() + 1; it < v.end(); ++it) {
if (*(it-1) != *it) {
summary.emplace_back(*it, 1);
} else {
++summary.back().second;
}
}
return summary;
}
int main() {
std::vector<double> s = {1, 2, 2, 3, 17, 1};
auto res = summarize(s);
auto resIt = summarizeIt(s);
return 0;
}
#包括
#包括
#包括
//基于索引的解决方案
模板
标准::向量汇总(标准::向量v){
if(v.empty())返回{};
性病媒概述;
排序(v.begin(),v.end());
//第一要素
T currentValue=*v.begin();
摘要:安置(std::vector{currentValue,static_cast(1)});
//解析从索引1开始的其余元素
对于(std::size_t i=1;i
如果使用std::map
这将变得微不足道。(无论如何,你不应该比较double
是否相等。)你正在阅读v[-1]
,这是一种未定义的行为。你知道使用无符号整数时0-1
是什么吗?我可以纠正,但它仍然是未定义的行为。你不应该对这样的问题使用索引。试着用迭代器来解决这个问题。我仍然不明白如何打印向量的值,因为当我使用cout时,它不允许我这样做
#include <utility>
#include <vector>
#include <algorithm>
// Index based solution
template<typename T>
std::vector<std::vector<double>> summarize(std::vector<T> v) {
if (v.empty()) return {};
std::vector<std::vector<double> > summary;
std::sort(v.begin(), v.end());
// first element
T currentValue = *v.begin();
summary.emplace_back(std::vector{currentValue, static_cast<T>(1)});
// parse the remaining elements starting with index 1
for (std::size_t i = 1; i < v.size(); ++i) {
if (currentValue != v.at(i)) {
currentValue = v.at(i);
summary.emplace_back(std::vector{currentValue, static_cast<T>(1)});
} else {
++summary.back().at(1);
}
}
return summary;
}
// Iterator and std::pair based solution
template<typename T>
std::vector<std::pair<T,std::size_t>> summarizeIt(std::vector<T> v) {
if (v.empty()) return {};
std::vector<std::pair<T,std::size_t>> summary;
std::sort(v.begin(), v.end());
summary.emplace_back(*v.begin(), 1);
for (auto it = v.begin() + 1; it < v.end(); ++it) {
if (*(it-1) != *it) {
summary.emplace_back(*it, 1);
} else {
++summary.back().second;
}
}
return summary;
}
int main() {
std::vector<double> s = {1, 2, 2, 3, 17, 1};
auto res = summarize(s);
auto resIt = summarizeIt(s);
return 0;
}