Skip to content

Commit cbe2347

Browse files
committed
Added sample solution for testing C++ 11
1 parent 4d5c078 commit cbe2347

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include<iostream>
2+
#include<random>
3+
#include<unordered_map>
4+
#include<vector>
5+
#include<algorithm>
6+
7+
void nptr(int x)
8+
{
9+
std::cout<<"This function shouldn't be called\n";
10+
}
11+
void nptr(char*x)
12+
{
13+
std::cout<<"This function should be called\n";
14+
if(x) std::cout<<"... but something is wrong!\n";
15+
}
16+
17+
constexpr long long fib(int x)
18+
{
19+
return x<2?1:fib(x-1)+fib(x-2);
20+
}
21+
22+
int main()
23+
{
24+
std::cout<<"If the program freezes now it's bad\n";
25+
const long long bigfib=fib(80);
26+
std::cout<<"It didn't froze\n";
27+
std::cout<<bigfib<<'\n';
28+
29+
std::mt19937 gen{std::random_device{}()};
30+
31+
std::vector<int> v;
32+
std::cout<<"Initialised\n";
33+
34+
v.resize((1<<18)/sizeof(int));
35+
std::cout<<"Allocated\n";
36+
37+
for(auto &x:v) x=gen();
38+
std::cout<<"Filled\n";
39+
40+
decltype(v) v2=v;
41+
std::cout<<"Copied\n";
42+
if(v.size()==v2.size()) std::cout<<"It's okay\n";
43+
else std::cout<<"It's not okay\n";
44+
v.clear();
45+
46+
v=std::move(v2);
47+
std::cout<<"Moved\n";
48+
if(v2.size()==0) std::cout<<"It's okay\n";
49+
else std::cout<<"It's not okay\n";
50+
51+
v.resize(1<<12);
52+
v.shrink_to_fit();
53+
v2.shrink_to_fit();
54+
55+
std::sort(std::begin(v), std::end(v), [](int a, int b){
56+
return abs(a)>abs(b);
57+
});
58+
std::cout<<"Sorted\n";
59+
60+
std::shuffle(std::begin(v), std::end(v), gen);
61+
std::cout<<"Shuffled\n";
62+
63+
std::unordered_map<int, char> um = {{1, 'a'}, {6, 'b'}, {2, 'x'}};
64+
std::uniform_int_distribution<char> letters('a', 'z');
65+
for(auto x:v)
66+
um.insert({x, letters(gen)});
67+
68+
nptr(nullptr);
69+
70+
std::vector<std::vector<char>> vvc={
71+
{'a', 'b', 'c'},
72+
{'d', 'e', 'f'},
73+
{'g', 'h', 'i'}
74+
};
75+
76+
std::tuple<int, char, std::vector<int>> tup;
77+
int x;
78+
v.resize(42);
79+
std::get<0>(tup) = 42;
80+
std::tie(x, std::ignore, v) = tup;
81+
if(x == 42)
82+
std::cout<<"Perfect!\n";
83+
else std::cout<<"Oh, noo! Something is wrong :(\n";
84+
85+
auto tup2 = std::tuple_cat(tup, std::make_tuple((double)3.5), tup, std::tie(x, x));
86+
}

0 commit comments

Comments
 (0)