[toc]

vcector

  • Store in the heap.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <vector>
struct Vertex
{
public:
float x, y, z;
Vertex(float x, float y, float z)
:x(x), y(y), z(z)
{

}

Vertex(const Vertex& vertex)
:x(vertex.x), y(vertex.y), z(vertex.z)
{
std::cout << "Copied!" << std::endl;
}


};

std::ostream& operator<<(std::ostream& stream, const Vertex& vertices)
{
stream << vertices.x << " " << vertices.y << " " << vertices.z;
return stream;
}


int main() {

std::vector<Vertex> vertices;
vertices.push_back({ 1,2,3 });
vertices.push_back({ 4,5,6 });
vertices.push_back({ 7,8,9 });


for (int i = 0; i < vertices.size(); i++)
std::cout << vertices[i] << std::endl;
for (Vertex& v : vertices)
std::cout << v << std::endl;
vertices.erase(vertices.begin() + 1);
for (Vertex& v : vertices)
std::cout << v << std::endl;
vertices.clear();

std::cin.get();
}
//output

vector strategy

1+2+3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main() {	
std::vector<Vertex> vertices;
vertices.reserve(3); //set the size of vector
vertices.push_back({ 1,2,3 });
vertices.push_back({ 4,5,6 });
vertices.push_back({ 7,8,9 });
// The constructor is called in the main function and then passed to the vector
std::cin.get();
}
//output
//Copied!
//Copied!
//Copied!



int main() {
std::vector<Vertex> vertices;
vertices.reserve(3); //set the size of vector
vertices.emplace_back(1,2,3 );
vertices.emplace_back(4,5,6 );
vertices.emplace_back(7,8,9 ); //Pass constructor arguments directly
std::cin.get();
}



//TODO

std::array

  • store in the stack.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<array>

template<typename T, int N>
void PrintArray(const std::array<T, N>& data) {
for (int i = 0; i < N; i++) {
std::cout << data[i] << " ";
}
std::cout << std::endl;
}

int main() {
std::array<int, 6> Array({ 1,2,3,4,5,6 });
PrintArray(Array);
}

thread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include <thread>

static bool is_Finished = false;

void Work()
{
using namespace std::literals::chrono_literals; //define ms,s
while (!is_Finished)
{
std::cout << "working" << std::endl;
std::this_thread::sleep_for(1s);
}
}
int main()
{
std::thread worker(Work);
std::cin.get();
is_Finished = true;
worker.join();
std::cin.get();
}

time

  • win32api QueryPerformanceCounter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include <chrono>
#include <thread>


struct Timer {
std::chrono::time_point < std::chrono::steady_clock> start,end;
std::chrono::duration<float> duration ;

Timer()
{
start = std::chrono::high_resolution_clock::now();
}


~Timer()
{
end = std::chrono::high_resolution_clock::now();
duration = end - start;
auto duration1 = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << duration << std::endl << duration1 << std::endl;
}
};

void hello()
{
Timer time;
for (int i = 0 ; i <100 ; i ++)
std::cout << "hello\n";
}

int main()
{
hello();
std::cin.get();
}

std::sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<algorithm>
#include<array>
#include<functional>

template<typename T,int N>
void print(const std::array<T,N>& data)
{
for(int i = 0; i < N; i ++)
std::cout << data[i] << " ";
std::cout << std::endl;

}

int main()
{
std::array<int,6> arr = { 1,7,11,5,1,6 };

std::sort(arr.begin(), arr.end(), std::greater<int>());
print(arr); //11 7 6 5 1 1

std::sort(arr.begin(), arr.end());
print(arr); //1 1 5 6 7 11

std::sort(arr.begin(), arr.end(), [](int a, int b)
{
if (a == 1)
return false;
if (b == 1)
return true;

return a > b;
});
print(arr); //11 7 6 5 1 1

std::cin.get();
}

std::function

//TODO