Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active May 17, 2024 14:12
Show Gist options
  • Save sdkfz181tiger/74205799361975603aaf74e1f565dcad to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/74205799361975603aaf74e1f565dcad to your computer and use it in GitHub Desktop.
C/C++課題ネタ09_テンプレートx可変長引数
//==========
// 任意個数の要素をコンテナに追加
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
template<typename C, typename... Args>
void pushAll(C &c, Args&&... args){
(c.push_back(args), ...);
}
int main(){
printf("Hello, Cpp!!\n");
vector<int> arr = {8, 6, 4, 3};
pushAll(arr, 1, 7, 2, 5, 9);
printf("Arr: ");
for(auto num: arr) printf("%d,", num);
printf("\n");
return 0;
}
//==========
// コンテナのany, all, none関数
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <stdio.h>
#include <array>
#include <list>
#include <vector>
using namespace std;
template<class C, class T>
bool contains(const C &c, const T &value){
return c.end() != find(c.begin(), c.end(), value);
}
template<class C, class... Args>
bool containsAny(const C &c, const Args &&... value){
return (... || contains(c, value));
}
template<class C, class... Args>
bool containsAll(const C &c, const Args &&... value){
return (... && contains(c, value));
}
template<class C, class... Args>
bool containsNone(const C &c, Args &&... value){
return !containsAny(c, forward<Args>(value)...);
}
int main(){
printf("Hello, Cpp!!\n");
const array<int, 6> myArr = {1, 2, 3, 4, 5};
const list<int> myList = {1, 2, 3, 4, 5};
const vector<int> myVec = {1, 2, 3, 4, 5};
if(contains(myArr, 4)){
printf("Contains!!\n");
}
if(containsAny(myArr, 2, 7, 8)){
printf("Contains any!!\n");
}
if(containsAll(myArr, 3, 4, 1)){
printf("Contains all!!\n");
}
if(containsNone(myArr, 6, 7, 8)){
printf("Contains none!!\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment