test/helloworld/zip.test.cpp
Depends on
Code
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <cassert>
#include <iostream>
#include <vector>
#include <string>
#include "../../Util/zip.hpp"
int main() {
using namespace std;
std::cout << "Hello World" << std::endl;
vector<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
vector<char> c{'a', 'b', 'c', 'd'};
vector<string> s{"zero", "one", "two"};
const vector<pair<int, char>> excpect1 = {
{0, 'a'}, {1, 'b'}, {2, 'c'}, {3, 'd'}
};
const vector<tuple<int, char, string>> excpect2 = {
{0, 'a', "zero"}, {1, 'b', "one"}, {2, 'c', "two"}
};
assert(zip(a, c) == excpect1);
assert(zip(a, c, s) == excpect2);
return 0;
}
#line 1 "test/helloworld/zip.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <cassert>
#include <iostream>
#include <vector>
#include <string>
#line 2 "Util/zip.hpp"
#include <algorithm>
#line 5 "Util/zip.hpp"
#include <utility>
#include <tuple>
/**
* @brief zip() (n個のvectorからn要素のタプルのvectorを生成する)
*/
template <class T1, class T2>
std::vector<std::pair<T1, T2>> zip(const std::vector<T1>& v1, const std::vector<T2>& v2) {
const auto sz = std::min(v1.size(), v2.size());
std::vector<std::pair<T1, T2>> ret(sz);
for (size_t i = 0; i < sz; ++i) ret[i] = std::make_pair(v1[i], v2[i]);
return ret;
}
template <class T1, class T2, class T3>
std::vector<std::tuple<T1, T2, T3>> zip(const std::vector<T1>& v1, const std::vector<T2>& v2, const std::vector<T3>& v3) {
const auto sz = std::min({v1.size(), v2.size(), v3.size()});
std::vector<std::tuple<T1, T2, T3>> ret(sz);
for (size_t i = 0; i < sz; ++i) ret[i] = std::make_tuple(v1[i], v2[i], v3[i]);
return ret;
}
#line 8 "test/helloworld/zip.test.cpp"
int main() {
using namespace std;
std::cout << "Hello World" << std::endl;
vector<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
vector<char> c{'a', 'b', 'c', 'd'};
vector<string> s{"zero", "one", "two"};
const vector<pair<int, char>> excpect1 = {
{0, 'a'}, {1, 'b'}, {2, 'c'}, {3, 'd'}
};
const vector<tuple<int, char, string>> excpect2 = {
{0, 'a', "zero"}, {1, 'b', "one"}, {2, 'c', "two"}
};
assert(zip(a, c) == excpect1);
assert(zip(a, c, s) == excpect2);
return 0;
}
Back to top page