728x90
1๋ฒ
#include<iostream>
#include <initializer_list>
template<typename T>
T average_list(std::initializer_list<T> il)
{
T sum = 0;
for (auto it=il.begin(); it!=il.end(); it++)
sum += *it;
return sum / il.size();
}
int main()
{
using namespace std;
auto q = average_list({ 15.4, 10.7, 9.0 });
cout << q << endl;
cout << average_list({ 20, 30, 19, 17, 45, 38 }) << endl;
auto ad = average_list<double>({ 'A', 70, 65.33 });
cout << ad << endl;
return 0;
}
2๋ฒ
#include<iostream>
#include <string>
#include <utility>
class Cpmv
{
public:
struct Info
{
std::string qcode;
std::string zcode;
};
private:
Info* pi;
public:
Cpmv();
Cpmv(std::string q, std::string z);
Cpmv(const Cpmv& cp);
Cpmv(Cpmv&& mv);
~Cpmv();
Cpmv& operator=(const Cpmv& cp);
Cpmv& operator=(Cpmv&& mv);
Cpmv operator+(const Cpmv& obj) const;
void Display() const;
};
Cpmv::Cpmv()
: pi(nullptr)
{
}
Cpmv::Cpmv(std::string q, std::string z)
: pi(new Info)
{
pi->qcode = q;
pi->zcode = z;
}
Cpmv::Cpmv(const Cpmv& cp)
{
pi->qcode = cp.pi->qcode;
pi->zcode = cp.pi->zcode;
}
Cpmv::Cpmv(Cpmv&& mv)
:pi(mv.pi)
{
std::cout << "์ด๋ ์์ฑ์ ์์ฑ && :\n";
mv.pi = nullptr;
}
Cpmv::~Cpmv()
{
if (pi) delete pi;
pi = nullptr;
}
Cpmv& Cpmv::operator=(const Cpmv& cp)
{
if (pi) delete pi;
pi = cp.pi;
return *this;
}
Cpmv& Cpmv::operator=(Cpmv&& mv)
{
std::cout << "์ด๋ ์์ฑ์ ์์ฑ = :\n";
pi = mv.pi;
mv.pi = nullptr;
return *this;
}
Cpmv Cpmv::operator+(const Cpmv& obj) const
{
std::string qcode;
std::string zcode;
if (pi) {
qcode += pi->qcode;
zcode += pi->zcode;
}
if (obj.pi) {
qcode += obj.pi->qcode;
zcode += obj.pi->zcode;
}
Cpmv temp(qcode, zcode);
return temp;
}
void Cpmv::Display() const
{
std::cout << "qcode: " << pi->qcode << " \t"
<< ", zcode: " << pi->zcode << '\n';
}
int main()
{
Cpmv one("q1", "z1");
std::cout << "one -> "; one.Display();
Cpmv two("q2", "z2");
std::cout << "two -> "; two.Display();
Cpmv three(Cpmv("q3", "z3"));
std::cout << "three -> "; three.Display();
Cpmv four;
four = one + two + three;
std::cout << "four -> "; four.Display();
return 0;
}
3๋ฒ
#include<iostream>
long double sum_values() { return 0; }
template<typename T,typename...Args>
long double sum_values(T value, Args...args)
{
long double sum = 0;
return value + sum_values(args...);
}
int main()
{
long double test;
test = sum_values(1.4, 2.5, 3.4, 'a');
std::cout << test << '\n';
return 0;
}
4๋ฒ
// functor.cpp -- ํํฌํฐ๋ฅผ ์ฌ์ฉํ๋ค
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
int main()
{
using std::list;
using std::cout;
using std::endl;
auto outint = [](int n) {cout << n << " "; };
int vals[10] = { 50, 100, 90, 180, 60, 210, 415, 88, 188, 201 };
list<int> yadayada(vals, vals + 10); // range ์์ฑ์
list<int> etcetera(vals, vals + 10);
// C++11์ ๋ค์์ ์ฝ๋๋ฅผ ๋์ ์ฌ์ฉํ ์ ์๋ค
// list<int> yadayada = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};
// list<int> etcetera {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};
cout << "์๋์ ๋ฆฌ์คํธ:\n";
for_each(yadayada.begin(), yadayada.end(), outint);
cout << endl;
for_each(etcetera.begin(), etcetera.end(), outint);
cout << endl;
yadayada.remove_if([](int n) {return n > 100; }); // ์ด๋ฆ์ด ์๋ ํจ์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ค
etcetera.remove_if([](int n) {return n > 200; }); // ํจ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
cout << "์ ๋น๋ ๋ฆฌ์คํธ:\n";
for_each(yadayada.begin(), yadayada.end(), outint);
cout << endl;
for_each(etcetera.begin(), etcetera.end(), outint);
cout << endl;
return 0;
}
728x90