为 map<T1,T2> 迭代器创建一个模板函数
Create a template function for map<T1,T2> iterators
所以我试图制作 4 个模板函数来执行以下操作:对通用容器求和、对映射求和、对通用容器迭代器求和以及对通用映射迭代器求和。我设法做到了前三个,但我无法弄清楚 sum map 迭代器。这是我的代码:
Sum.hpp
#ifndef SUM_HPP
#define SUM_HPP
#include <iostream>
#include <map>
#include <string>
using namespace std;
//Sum Generic Container
template <typename T>
double Sum(const T& cont) {
double sum = 0;
typename T::const_iterator iter = cont.begin();
for (iter; iter != cont.end(); iter++) {
sum += *iter;
}
return sum;
}
//Sum Generic Map
template <typename T1, typename T2>
double Sum(const map<T1, T2>& mp) {
double sum = 0;
typename map<T1,T2>::const_iterator iter = mp.begin();
for (iter; iter != mp.end(); iter++) {
sum += iter->second;
}
return sum;
}
//Sum Generic Container Iterators
template <typename T>
double Sum(T& begin, const T& end) {
double sum = 0;
for (begin; begin != end; begin++) {
sum += *begin;
}
return sum;
}
//Sum Generic Map Iterators
template <typename T1, typename T2>
double Sum(map<T1, T2>::iterator& begin, map<T1, T2>::iterator& end) {
double sum = 0;
for (begin; begin != end; begin++) {
sum += begin->second;
}
return sum;
}
#endif
Test.cpp:
#include "Sum.hpp"
#include <iostream>
#include <map>
using namespace std;
int main() {
//Map
cout << "map" << endl;
map<string, double> mp;
mp["weight"] = 5.5;
mp["height"] = 6.7;
mp["length"] = 8.4;
map<string, double>::iterator mp_iter_begin = mp.begin();
map<string, double>::iterator mp_iter_end = mp.end();
cout << Sum(mp_iter_begin, mp_iter_end) << endl;
return 0;
}
当我 运行 Sum() 函数尝试调用 Sum Generic Container Iterators 函数时,如果我注释掉 Sum Generic Container Iterators 函数,我会收到 "no instance of overloaded function" 错误。谁能发现我做错了什么?
1) The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id:
也就是说对于map<T1, T2>::iterator
,模板参数T1
和T2
无法推导。
您可以通过SFINAE解决问题,例如
//Sum Generic Container Iterators
template <typename T>
auto Sum(T begin, T end) -> remove_reference_t<decltype(*begin += *begin)> {
remove_reference_t<decltype(*begin)> sum = 0;
for (; begin != end; begin++) {
sum += *begin;
}
return sum;
}
//Sum Generic Map Iterators
template <typename T>
auto Sum(T begin, T end) -> remove_reference_t<decltype(begin->second += begin->second)> {
remove_reference_t<decltype(begin->second)> sum = 0;
for (; begin != end; begin++) {
sum += begin->second;
}
return sum;
}
我想出了如何按照我想要的方式进行专业化:
//Sum Generic Container Iterators
template <typename T>
double Sum(T& begin, const T& end) {
double sum = 0;
for (begin; begin != end; begin++) {
sum += *begin;
}
return sum;
}
//Sum Map<string, double> Iterators
template <>
double Sum(map<string, double>::iterator& begin, const map<string, double>::iterator& end) {
double sum = 0;
for (begin; begin != end; begin++) {
sum += begin->second;
}
return sum;
}
请注意确保通用容器迭代器和映射迭代器函数具有匹配参数(两个参数都是引用,第二个参数是两个模板函数的常量)或 map<string,double>
的特化的重要性工作。
所以我试图制作 4 个模板函数来执行以下操作:对通用容器求和、对映射求和、对通用容器迭代器求和以及对通用映射迭代器求和。我设法做到了前三个,但我无法弄清楚 sum map 迭代器。这是我的代码:
Sum.hpp
#ifndef SUM_HPP
#define SUM_HPP
#include <iostream>
#include <map>
#include <string>
using namespace std;
//Sum Generic Container
template <typename T>
double Sum(const T& cont) {
double sum = 0;
typename T::const_iterator iter = cont.begin();
for (iter; iter != cont.end(); iter++) {
sum += *iter;
}
return sum;
}
//Sum Generic Map
template <typename T1, typename T2>
double Sum(const map<T1, T2>& mp) {
double sum = 0;
typename map<T1,T2>::const_iterator iter = mp.begin();
for (iter; iter != mp.end(); iter++) {
sum += iter->second;
}
return sum;
}
//Sum Generic Container Iterators
template <typename T>
double Sum(T& begin, const T& end) {
double sum = 0;
for (begin; begin != end; begin++) {
sum += *begin;
}
return sum;
}
//Sum Generic Map Iterators
template <typename T1, typename T2>
double Sum(map<T1, T2>::iterator& begin, map<T1, T2>::iterator& end) {
double sum = 0;
for (begin; begin != end; begin++) {
sum += begin->second;
}
return sum;
}
#endif
Test.cpp:
#include "Sum.hpp"
#include <iostream>
#include <map>
using namespace std;
int main() {
//Map
cout << "map" << endl;
map<string, double> mp;
mp["weight"] = 5.5;
mp["height"] = 6.7;
mp["length"] = 8.4;
map<string, double>::iterator mp_iter_begin = mp.begin();
map<string, double>::iterator mp_iter_end = mp.end();
cout << Sum(mp_iter_begin, mp_iter_end) << endl;
return 0;
}
当我 运行 Sum() 函数尝试调用 Sum Generic Container Iterators 函数时,如果我注释掉 Sum Generic Container Iterators 函数,我会收到 "no instance of overloaded function" 错误。谁能发现我做错了什么?
1) The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id:
也就是说对于map<T1, T2>::iterator
,模板参数T1
和T2
无法推导。
您可以通过SFINAE解决问题,例如
//Sum Generic Container Iterators
template <typename T>
auto Sum(T begin, T end) -> remove_reference_t<decltype(*begin += *begin)> {
remove_reference_t<decltype(*begin)> sum = 0;
for (; begin != end; begin++) {
sum += *begin;
}
return sum;
}
//Sum Generic Map Iterators
template <typename T>
auto Sum(T begin, T end) -> remove_reference_t<decltype(begin->second += begin->second)> {
remove_reference_t<decltype(begin->second)> sum = 0;
for (; begin != end; begin++) {
sum += begin->second;
}
return sum;
}
我想出了如何按照我想要的方式进行专业化:
//Sum Generic Container Iterators
template <typename T>
double Sum(T& begin, const T& end) {
double sum = 0;
for (begin; begin != end; begin++) {
sum += *begin;
}
return sum;
}
//Sum Map<string, double> Iterators
template <>
double Sum(map<string, double>::iterator& begin, const map<string, double>::iterator& end) {
double sum = 0;
for (begin; begin != end; begin++) {
sum += begin->second;
}
return sum;
}
请注意确保通用容器迭代器和映射迭代器函数具有匹配参数(两个参数都是引用,第二个参数是两个模板函数的常量)或 map<string,double>
的特化的重要性工作。