c_cpp 广度优先搜索(C ++)

[参考链接](https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/)

bfs.cpp
// Program to print BFS traversal from a given 
// source vertex. BFS(int s) traverses vertices 
// reachable from s. 
#include<iostream> 
#include <list> 

using namespace std; 

// This class represents a directed graph using 
// adjacency list representation 
class Graph 
{ 
	int V; // No. of vertices 

	// Pointer to an array containing adjacency 
	// lists 
	list<int> *adj; 
public: 
	Graph(int V); // Constructor 

	// function to add an edge to graph 
	void addEdge(int v, int w); 

	// prints BFS traversal from a given source s 
	void BFS(int s); 
}; 

Graph::Graph(int V) 
{ 
	this->V = V; 
	adj = new list<int>[V]; 
} 

void Graph::addEdge(int v, int w) 
{ 
	adj[v].push_back(w); // Add w to v’s list. 
} 

void Graph::BFS(int s) 
{ 
	// Mark all the vertices as not visited 
	bool *visited = new bool[V]; 
	for(int i = 0; i < V; i++) 
		visited[i] = false; 

	// Create a queue for BFS 
	list<int> queue; 

	// Mark the current node as visited and enqueue it 
	visited[s] = true; 
	queue.push_back(s); 

	// 'i' will be used to get all adjacent 
	// vertices of a vertex 
	list<int>::iterator i; 

	while(!queue.empty()) 
	{ 
		// Dequeue a vertex from queue and print it 
		s = queue.front(); 
		cout << s << " "; 
		queue.pop_front(); 

		// Get all adjacent vertices of the dequeued 
		// vertex s. If a adjacent has not been visited, 
		// then mark it visited and enqueue it 
		for (i = adj[s].begin(); i != adj[s].end(); ++i) 
		{ 
			if (!visited[*i]) 
			{ 
				visited[*i] = true; 
				queue.push_back(*i); 
			} 
		} 
	} 
} 

// Driver program to test methods of graph class 
int main() 
{ 
	// Create a graph given in the above diagram 
	Graph g(4); 
	g.addEdge(0, 1); 
	g.addEdge(0, 2); 
	g.addEdge(1, 2); 
	g.addEdge(2, 0); 
	g.addEdge(2, 3); 
	g.addEdge(3, 3); 

	cout << "Following is Breadth First Traversal "
		<< "(starting from vertex 2) \n"; 
	g.BFS(2); 

	return 0; 
} 

c_cpp C / C ++中常量

[参考链接](https://qiaoxu123.github.io/post/3-C++Courses/) <br/>`const'变量在C / C ++中有较大的区别。对于C语言来讲, `const`定义仅仅是一个只读变量,可以通过指针进行修改。而C ++中对`const`参数进行了升级处理,使用类似宏定义的方式进行了常量化。为了向上兼容,也保留了原有的一些性质。具体看参考链接。

c_const.c
#include<stdio.h>

int main(){
	const int c = 0;
	int* p = (int*)&c;
	
	printf("Begin...\n");
	*p = 5;
	printf("c = %d\n",c);
	printf("End...\n");
	return 0;
} 
cplusplus_const.cpp
#include<iostream>
using namespace std;

void f(){
	#define a 3		//此处为预处理,在编译之前已经将所有a都进行了替换
	const int b = 4;
}

void g(){
	printf("a = %d\n",a); //此处a已经进行了替换
	//printf("b = %d\n",b);
}

int main(){
	const int A = 1;
	const int B = 1;
	int array[A + B] = {0};//此处若用C语言编译器,则报错。因为数组大小定义使用变量
	int i = 0;
	for(i = 0;i < (A + B);++i)
		printf("array[%d] = %d\n",i,array[i]);
	f();
	g();
	return 0;
}

c_cpp 向量

parenthesis.c
prevPoints.push_back(vector<int>(1,C1));
return vector<int> ();
unique.c
v.erase(unique(v.begin(), v.end()), v.end());

c_cpp 分类

sort.cpp
sort(conections.begin(),conections.end(),
[](const vector<int> &a, const vector<int> &b) { return a[2] < b[2]; } );
//比较二维数组的第二列
sort.c
// Ascending order
sort(v.begin(), v.end());
sort(v.begin(), v.end(), less<int>());

// Descending order
sort(v.rbegin(), v.rend());
sort(v.begin(), v.end(), greater<int>());

c_cpp Atlas300代码片段

1.cpp
    ImageData<uint8_t> resized_image;
    int out_size = ALIGN_UP(input_tensor_dim_vec_[model_id].w, 128) * ALIGN_UP(input_tensor_dim_vec_[model_id].h, 16) * 3/2;
    uint8_t * out_buffer = (uint8_t *)memalign(128, out_size);
    if (out_buffer == NULL)
    {
        HIAI_ENGINE_LOG("Hfbc2Yuv call memalign failed");
        return HIAI_ERROR;
    }
    resized_image.size = out_size;
    resized_image.width = input_tensor_dim_vec_[model_id].w;
    resized_image.height = input_tensor_dim_vec_[model_id].h;
    resized_image.data.reset(out_buffer, std::default_delete<uint8_t []>());

c_cpp C ++显式参数作用

C ++中显参参的作用是为了防止C ++函数在执行时进行隐式转换。下载代码会报错,注释部分不会报错。<br/> [参考地址](https://blog.csdn.net/tiefanhe /条/信息/ 11478901)

test.cpp
#include<iostream>

// class Base {
// public:
//     Base() = default;
//     Base(int num){};
// private:
//     int num;
// };

class Base {
public:
    Base() = default;
    explicit Base(int num);
private:
    int num;
};

int main() {
    Base obj = 10;
    return 0;
}

c_cpp C ++ 11标准新特性:Defaulted和Deleted函数

`default`函数存在的问题,在于用户想要自定义函数进行类的初始化。此时类的默认构造函数需要显示进行定义。这是用户自定义的无参数的默认构造函数<br/>此时定义的默认构造函数的功能和类模板原始的自定义构造函数的功能是相同的,但是效率变得非常低。在这使用`default`关键字,将用户自定义的无参数<br/>的构造函数进行处理,这样编译器在处理的时候可以极大地提升效率。<br/> <br/> --- <br/> <br/> [参考链接](https://www.ibm。 COM / developerWorks中国/ CN / AIX /库/ 1212_lufang_c11new / index.html的)

default.cpp
#include <iostream>
using namespace std;

class base {
public:
    base() = default;
    ~base() = default;
private:
    int num;
};


int main() {
    cout << "Hello World" << endl;
    return 0;
}
default2.cpp
#include <iostream>

class X {
public:
    virtual ~X() = default;    
private:
    int x;
};

class Y:public X {
private:
    int y;
};

int main(){
    X* x = new Y;
    delete(x);
}

c_cpp COUT

cout.cpp
#include <bitset>
#include <iostream>
using namespace std;

int main() {
	cout << "36的8进制:	" << std::oct << 36 << endl;	// 44
	cout << "36的10进制:	" << std::dec << 36 << endl;	// 36
	cout << "36的16进制:	" << std::hex << 36 << endl;	// 24
	cout << "36的2进制:	" << bitset<8>(36) << endl;		// 00100100
	return 0;
}

c_cpp _strlwr&_strupr

_strlwr_strupr.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	char str1[] = "China";
	cout << _strlwr(str1) << endl;	// china
	cout << _strupr(str1) << endl;	// CHINA
}

c_cpp strlen的()

size_t strlen(const char * str); <br/> <br/>获取字符串长度<br/>返回C字符串str的长度。 <br/> <br/> C字符串的长度由终止空字符确定:AC字符串与字符串开头和终止空字符之间的字符数一样长(不包括终止空字符)人物本身)。 <br/> <br/>这不应该与保存字符串的数组的大小混淆。例如:<br/> <br/> char mystr [100] =“test string”; <br/> <br/>定义了一个大小为100个字符的字符数组,但是初始化了mystr的C字符串的长度只有11个字符。因此,当sizeof(mystr)求值为100时,strlen(mystr)返回11. <br/> <br/>在C ++中,char_traits :: length实现相同的行为。

strlen.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	cout << strlen("abcdefg") << endl;	// 7
}