1. 程式人生 > 其它 >元宇宙等於數字孿生?——元宇宙與數字孿生關係淺析

元宇宙等於數字孿生?——元宇宙與數字孿生關係淺析

基礎

//ASCII
'A':65
'Z':90
'a':97
'z':122
'0':48

常見函式

abs()
reverse(a, a + n)	//#include<algorithm>
memset(a, 0, sizeof a)	//#include<cstring>
memcpy(b, a, sizeof a);	//#include<cstring>

字串

//char
char s[100];
fgets(s, 100, stdin);
cin.getline(s, 100);

//string
string s;
getline(cin, s);

//輸出
printf("%s", s.c_str());

//遍歷
for(int i = 0; i < s.size(); i++)

for(char c : s)
    cout << c << endl;

for(auto c : s)
    cout << c << endl;

for(char &c : s)    //如果要改變

//刪除最後一個字元
s.pop_back();

//擷取
s.substr(起始位置, 長度);	//長度不寫則表示到結尾

//插入
s.insert(起始位置, string);

//stringstream-<sstream>
string s = "123 abc 23 1.4";
stringstream ssin(s);
int a, b; string str; double c;
// a = 123, b = 23, str = "abc", c = 1.4;
ssin >> a >> str >> b >> c;
cout << a << ' ' << str << ' ' << b << ' ' << c;

//str.back()
str.back() //返回字串最後一個字元

//字串陣列
string s[100];
while (cin >> str[n])
    n++;

//大小寫轉換
for(auto &c : a)
    c = tolower(c);
for(auto &c : a)
    c = toupper(c);

//find()函式
//npos是一個常數,用來表示不存在的位置
if(a.find(b) != string::npos)

函式

//二維陣列的第二維下標不可以省略
void output(int m, int n, int a[][3])

技巧

//1
for(int i = 0, len = strlen(str); i < len; i++)

//2、第一類雙指標
int j = i;
while(j < s.size() && s[j] == s[i])
    j++;
i = j - 1;	//for迴圈i++

指標

// . 和 -> 的區別,主要看p是哪種型別的變數
Node p = new Node();
p.next
p.val

Node* p = new Node();
p->next
p->val

STL容器

vector

#include<vector>

//初始化
vector<int> a = {1, 2, 3, 4, 5};
vector<int> a({1, 2, 3, 4, 5});
vector<vector<int>> num(n, vector<int>(n));    //行n,列n

//遍歷
//方式1:通過a[i]讀取元素值
for (int i = 0; i < a.size(); i++) 
    cout << a[i] << endl;

//方式2:迭代器,通過*i讀取元素值
for (vector<int>::iterator i = a.begin(); i < a.end(); i++) 
    cout << *i << endl;

//方式3:迭代器簡化版
for (auto i = a.begin(); i < a.end(); i++) 
    cout << *i << endl;

//方式4:auto
for (auto x : a)
    cout << x << << endl; 

//操作
a[k];           // 取值
a.size();       // 長度
a.empty();      // 判空
a.clear();      // 清空

a.push_back();  //尾部插入
a.pop_back();   //尾部刪除
a.front();      //獲取頭部元素
a.back();       //獲取尾部元素

//在指定範圍內大於等於x的元素下標
int index = lower_bound(a.begin(), a.end(), 2) - a.begin();    //index值為1
//在指定範圍內大於x的元素下標
int index = lower_bound(a.begin(), a.end(), 2) - a.begin();    //index值為2

//反向迭代器
for(auto i = a.rbegin(); i < a.rend(); i++)
        cout << *i << endl;

return vector<int>(res.rbegin(), res.rend());

queue

#include<queue>

/**********普通佇列**********/
//定義
queue<int> q;

//操作
q.push(1);    //隊尾入隊
q.pop();      //對頭出隊
q.front();    //取隊頭
q.back();     //取隊尾

/**********優先佇列**********/
//定義
priority_queue<int> a;    //大根堆
priority_queue<int, vector<int>, greater<int>> b;    //小根堆

//操作
a.push(1);    //插入
a.top();      //取最大值
a.pop();      //刪除最大值

//自定義型別
struct Rec {
    int a, b;

    //大根堆需要自定義類過載<號
    bool operator< (const Rec& t) const {
        return a < t.a;
    }

    //小根堆需要自定義類過載>號
    bool operator> (const Rec& t) const {
        return a > t.a;
    }
};

priority_queue<Rec> c;    //大根堆
priority_queue<Rec, vector<Rec>, greater<Rec>> d;    //小根堆
d.push({1, 2});

deque

#include<deque>

// 定義
deque<int> q;

// 操作
q[i]                // 隨機訪問
q.begin();          // 隊頭元素地址,用*q.begin()讀取元素
q.end();            // 隊尾元素地址,用*q.end()讀取元素
q.front();          // 隊頭元素值
q.back();           // 隊尾元素值

push_back();        // 隊尾插入元素
push_front();       // 隊頭插入元素
pop_back();         // 隊尾刪除元素
pop_front();        // 隊頭刪除元素

stack

#include <stack>

// 定義
stack<int> stk;

// 操作
s.push(x);       // 入棧
s.top();         // 檢視棧頂
s.pop();         // 出棧

set

#include<set>

// 定義
set<int> s;             // 集合
multiset<int> ms;       // 允許元素重複

//自定義類要求過載<

// 操作
s.size();
s.empty();
s.claer();

s.insert(x);
s.find(x);              // 返回迭代器,可用if(s.find(x) == s.end())判斷是否存在元素x
s.lower_bound(x);       // 返回大於等於x的最小元素
s.upper_bound(x);       // 返回大於x的最小元素

s.erase(x);             // 刪除x
s.count(x);             // 統計x出現的次數(普通集合只會返回0或1,multiset可能返回大於1的數)

map

#include<map>

//定義
map<string, int> a;
a["ww"] = 2;

map<string, vector<int>> a;
a["ww"] = vector<int>({1, 2, 3, 4});
cout << a["ww"][2] << endl;

unordered_map

#include <unordered_map>

//定義
unordered_map<int, int> hash;

//賦值
hash[0] = 1;
hash[1] = 2;

//查詢
hash[0];

//遍歷key是否存在,存在返回1,否則返回0
hash.count(1) != 0

//遍歷
for (unordered_map<int, int>::iterator it = hash.begin(); it != hash.end(); it ++ )
	cout << it->first << ' ' << it->second << endl;

bitset

#include <bitset>

//定義二進位制串
bitset<100> s;

//操作
s[0] = 1;
s.count();      // 1的個數
s.set(p);       // 第p位設為1
s.reset(p);     // 第p位設為0

pair

pair<int, string> a;
a = {88, "ww"};
cout << a.first << " " << a.second << endl;

位運算

  • 右移:>>\(\frac{a}{{{2^k}}}\)
  • 左移:<<\(a*{2^k}\)
  • x的第k位數字:x >> k & 1
  • lowbit(x) = x & -x ,返回x的最後一位1

常用庫函式

#include<algorithm>

//reverse()--反轉
vector<int> a({1, 2, 3, 4, 5});    //vector
reverse(a.begin(), a.end());

int a[] = {1, 2, 3, 4, 5};    //陣列
reverse(a, a + 5);

//unique()--必須保證相同元素捱到一起,unique()並沒有真的刪除重複元素,它僅將非重複的元素移到了前面
int a[] = {1, 2, 3, 4, 5};
unique(a, a + a.size());    //去重後最後一個元素的下一個元素的地址
int m = unique(a, a + 5) - a;    //陣列中不同元素的個數

int m = unique(a.begin(), a.end()) - a.begin();    //vector中不同元素的個數

a.erase(unique(a.begin(), a.end()), a.end());    //去除重複元素

//random_shuffle--隨機打亂
#include<ctime>
srand(time(0));
random_shuffle(a.begin(), a.end());

//sort()--排序
sort(a.begin(), a.end());    //從小到大
sort(a.begin(), a.end(), greater<int>());    //從大到小

//自定義排序規則
bool cmp(int a, int b)  //a是否應該排在b的前面
{
    return a > b;    //如果a小於b,那麼a應該排到b的前面
}
sort(a.begin(), a.end(), cmp);

//結構體自定義排序規則
struct rec 
{
    int x, y;   
};
bool cmp(rec a, rec b)  //a是否應該排在b的前面
{
    return a.x < b.y;    //如果a小於b,那麼a應該排到b的前面
}
struct rec r[5];
sort(r, r + 5, cmp);

//過載 < 號
struct rec 
{
    int x, y;   
    bool operator< (const rec &t) const    //此處的 < 不可修改
    {
        return x > t.x;    //排序方式只能修改此處的 < 或 >
    }
};
struct rec r[5];
sort(r, r + 5);

//lower_bound--返回大於等於x的最小元素的迭代器
int t = lower_bound(a, a + 5, 3) - a;

//upper_bound--返回大於等於x的最小元素的迭代器