我需要撰寫一個通用函式來查找屬于兩個容器的元素的總和,并將這些元素放在一個向量中,該向量的型別是總和的結果。
例子:
容器一:5 2 8 3 6
容器二:6 1 5
總和容器:11 3 13 3 6
#include <iostream>
#include <cmath>
#include <vector>
template < typename tip1, typename tip2, typename tip >
tip sum_of_containers(tip1 blok1, tip2 blok2) {
std::vector < tip > a;
int n1 = std::distance(blok1.begin(), blok1.end());
int n2 = std::distance(blok2.begin(), blok2.end());
int n;
n = n1;
if (n2 > n1) n = n2;
for (int i = 0; i < n; i )
a[i].push_back(blok1[i] blok2[i]);
return a;
}
int main() {
int n1, n2, x;
std::cin >> n1;
std::vector < double > a, b, c;
for (int i = 0; i < n1; i ) {
std::cin >> x;
a.push_back(x);
}
std::cin >> n2;
for (int i = 0; i < n2; i ) {
std::cin >> x;
b.push_back(x);
}
c = sum_of_containers(a, b);
for (double i: c)
std::cout << i << " ";
return 0;
}
第 31 行的錯誤:
沒有匹配函式呼叫“sum_of_containers”
無法推斷出模板引數“tip”
你能給我一些方法或想法如何解決這個問題嗎?
uj5u.com熱心網友回復:
這個問題非常不清楚。
“通用”可能意味著不同的容器,例如std::vector或std::deque。但是在您的函式函式中,您使用的是 astd::vector和索引運算子[]和 function push_back。僅有的 2 個具有所有這些的容器是std::vector和std::deque。所以,這沒有多大意義。
“通用”的下一個級別是為 a 提供不同的資料型別std::vector。但是對于 3 個模板引數,這意味著最壞的情況是我們添加 2 種不同的資料型別并將它們分配給第 3 種不同的資料型別。
從邏輯上講,這會產生很多其他麻煩,需要型別轉換,結果可能是精度損失。
如果我們查看 main 函式,我們會看到,std::vectors所有具有相同資料型別的 3double都被實體化了。這是有道理的。std::vector這將限制“通用”函式為 a將持有的型別提供一個通用模板化引數。
這可能如下所示:
#include <iostream>
#include <vector>
#include <algorithm>
template <typename T>
std::vector<T> sumOfVectors(const std::vector<T>& t1, const std::vector<T>& t2) {
// Get a reference to the larger vector
const std::vector<T>& largerVector = (t1.size() > t2.size()) ? t1 : t2;
// Create the resulting vector that can hold all elements
std::vector<T> result(largerVector.size(), {});
size_t index{};
for (; index < std::min(t1.size(),t2.size()); index)
result[index] = t1[index] t2[index];
for (; index < largerVector.size(); index)
result[index] = largerVector[index];
return result;
}
int main() {
int n1, n2, x;
std::cin >> n1;
std::vector < double > a, b, c;
for (int i = 0; i < n1; i ) {
std::cin >> x;
a.push_back(x);
}
std::cin >> n2;
for (int i = 0; i < n2; i ) {
std::cin >> x;
b.push_back(x);
}
c = sumOfVectors(a, b);
for (double i : c)
std::cout << i << " ";
return 0;
}
但是當然你會為此定義一個運算子 ,這給我們一個更直觀的結果:
#include <iostream>
#include <vector>
#include <algorithm>
template <typename T>
std::vector<T> operator (const std::vector<T>& t1, const std::vector<T>& t2) {
// Get a reference to the larger vector
const std::vector<T>& largerVector = (t1.size() > t2.size()) ? t1 : t2;
// Create the resulting vector that can hold all elements
std::vector<T> result(largerVector.size(), {});
size_t index{};
for (; index < std::min(t1.size(),t2.size()); index)
result[index] = t1[index] t2[index];
for (; index < largerVector.size(); index)
result[index] = largerVector[index];
return result;
}
int main() {
int n1, n2, x;
std::cin >> n1;
std::vector < double > a, b, c;
for (int i = 0; i < n1; i ) {
std::cin >> x;
a.push_back(x);
}
std::cin >> n2;
for (int i = 0; i < n2; i ) {
std::cin >> x;
b.push_back(x);
}
c = a b;
for (double i : c)
std::cout << i << " ";
return 0;
}
編輯
通過評論中給出的確切任務描述,我們可以提出所需的解決方案。
它有點重。
我們甚至可以添加型別特征來檢查容器是否是可迭代的,但這可能太多了(盡管在 C 20 中很容易使用類似的東西if constexpr (std::ranges::range<Container>)
無論如何,請查看帶有 2 個測驗用例的更新解決方案。
#include <iostream>
#include <type_traits>
#include <vector>
#include <deque>
#include <forward_list>
#include <array>
#include <list>
#include <string>
// Some aliases to avoid heavy typing
template <typename T, typename U>
using Value_t = typename std::common_type<typename T::value_type, typename U::value_type>::type;
template <typename T, typename U>
using Vector_t = typename std::vector<Value_t<T, U>>;
template <typename T, typename U>
auto sum_of_containers(const T& c1, const U& c2) -> Vector_t<T, U> {
// Get rid of template parameters using aliases
using MyType = Value_t<T,U>;
using MyVector = Vector_t<T, U>;
// Here we will store the result
MyVector result{};
typename T::const_iterator c1Iter = std::begin(c1);
typename U::const_iterator c2Iter = std::begin(c2);
// Add, as long as there are the same number of elements in the containers
while ((c1Iter != std::end(c1)) and (c2Iter != std::end(c2)))
result.push_back(static_cast<MyType>(*c1Iter ) static_cast<MyType>(*c2Iter ));
// If there should still be elements in one of the containers, then add them to the resulting vector as is
while (c1Iter != std::end(c1))
result.push_back(static_cast<MyType>(*c1Iter ));
while (c2Iter != std::end(c2))
result.push_back(static_cast<MyType>(*c2Iter ));
return result;
}
int main() {
// Test Data 0
std::deque<float> fl1 { 0.1f, 0.2f, 0.3f };
std::deque<double> dbl1 { 0.1, 0.2, 0.3, 0.4, 0.5 };
auto result0 = sum_of_containers(fl1, dbl1);
for (const auto& x0 : result0)
std::cout << x0 << '\n';
std::cout << '\n';
// Test Data 1
std::deque<int> dq{ -1,2,-3 };
std::forward_list<float> fl{ 0.1f, 0.2f, 0.3f, 0.4f, 0.5f };
auto result1 = sum_of_containers(dq, fl);
for (const auto& x1 : result1)
std::cout << x1 << '\n';
std::cout << '\n';
// Test Data 2
std::array<unsigned long, 3> ar1{ 1ul,2ul,3ul };
std::list<double> dbl{ 0.1, 0.2, 0.3, 0.4, 0.5 };
auto result2 = sum_of_containers(ar1, dbl);
for (const auto& x2 : result2)
std::cout << x2 << '\n';
std::cout << '\n';
}
.
.
.
當然,但不是必需的,您也可以在這里實作 運算子。
#include <iostream>
#include <type_traits>
#include <vector>
#include <deque>
#include <forward_list>
#include <array>
#include <list>
#include <string>
// Some aliases to avoid heavy typing
template <typename T, typename U>
using Value_t = typename std::common_type<typename T::value_type, typename U::value_type>::type;
template <typename T, typename U>
using Vector_t = typename std::vector<Value_t<T, U>>;
template <typename T, typename U>
auto operator (const T& c1, const U& c2) -> Vector_t<T, U> {
// Get rid of template parameters using aliases
using MyType = Value_t<T, U>;
using MyVector = Vector_t<T, U>;
// Here we will store the result
MyVector result{};
typename T::const_iterator c1Iter = std::begin(c1);
typename U::const_iterator c2Iter = std::begin(c2);
// Add, as long as there are the same number of elements in the containers
while ((c1Iter != std::end(c1)) and (c2Iter != std::end(c2)))
result.push_back(static_cast<MyType>(*c1Iter ) static_cast<MyType>(*c2Iter ));
// If there should still be elements in one of the containers, then add them to the resulting vector as is
while (c1Iter != std::end(c1))
result.push_back(static_cast<MyType>(*c1Iter ));
while (c2Iter != std::end(c2))
result.push_back(static_cast<MyType>(*c2Iter ));
return result;
}
int main() {
// Test Data 0
std::deque<float> fl1{ 0.1f, 0.2f, 0.3f };
std::deque<double> dbl1{ 0.1, 0.2, 0.3, 0.4, 0.5 };
auto result0 = fl1 dbl1;
for (const auto& x0 : result0)
std::cout << x0 << '\n';
std::cout << '\n';
// Test Data 1
std::vector<int> ve{ -1,2,-3 };
std::forward_list<float> fl{ 0.1f, 0.2f, 0.3f, 0.4f, 0.5f };
auto result1 = ve fl;
for (const auto& x1 : result1)
std::cout << x1 << '\n';
std::cout << '\n';
// Test Data 2
std::array<unsigned long, 3> ar1{ 1ul,2ul,3ul };
std::list<double> dbl2{ 0.1, 0.2, 0.3, 0.4, 0.5 };
auto result2 = ar1 dbl2;
for (const auto& x2 : result2)
std::cout << x2 << '\n';
std::cout << '\n';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/464408.html
