[精讚] [會員登入]
583

【C++】一些好用的C++小功能 —— 壹

不太重要,但是可以增加程式質量以及可以讓程式看起來好像很厲害

分享此文連結 //n.sfs.tw/14947

分享連結 【C++】一些好用的C++小功能 —— 壹@小編過路君子
(文章歡迎轉載,務必尊重版權註明連結來源)
2021-03-02 19:53:53 最後編修
2021-02-28 20:44:21 By 過路君子
 

哈囉大家好,我是不知道為什麼開始都不發輕鬆文章的過路君子

最近找到了不錯的遊戲,找時間來寫如何呢?

 

以下的程式會盡可能#include最少的標頭檔來完成所需的效果。

當然某些小功能可以透過更多的標頭檔來更簡單的完成,一切都依照個人喜好,小編僅提供一種想法,畢竟簡單和隨心所欲才是寫程式最大的樂趣嘛!

限定小數輸出位數

#include<iostream>

using namespace std;

int main()
{
	double a = 3.1415926;
	
	cout << a << '\n';
	cout.precision(3);
	cout << a;
	
	return 0;
}

輸出

3.14159
       3.14

 

使游標從某處開始

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s;
	
	cout << "Type a word: ______\b\b\b\b\b\b";
	cin >> s;
	cout << s;
	
	return 0;
}

輸出

Type a word: Hoo___
      Hoo

 

只有在標頭檔(.h)的內容還未包含到程式中時
才會包含這些內容

#ifndef HEAD-NAME
#define HEAD-NAME
//Header file contents go here
#endif

 

判斷char內文字型態(C也可用)

#include<iostream>
#include<cctype>

using namespace std;

int main()
{
	char str[5] = "o 3!";
	
	for(int b=0;11>b;b++)
	{
		if(isalpha(str[b])) cout << "type:alpha " << str[b] << '\n';  //是否為英文字母
		if(isdigit(str[b])) cout << "type:digit " << str[b] << '\n';  //是否為數字
		if(isspace(str[b])) cout << "type:space " << str[b] << '\n';  //是否為空白
		if(ispunct(str[b])) cout << "type:punct " << str[b] << '\n';  //是否為標點符號
	}
	
	return 0;
}

輸出

type:alpha o
       type:space
       type:digit 3
       type:punct !

 

定義常數

//C風格的定義方式
#define UNI 6

//C++風格的定義方式
const int UNI = 6;

//C++定義常數的優點:
//一、是允許型態檢查
//二、可以對陣列或是結構使用

 

同位組合(union)

#include<iostream>
using namespace std;

union SomeValue
{
	int hello;
	int world;
	int uiuxp;
};

int main()
{
	SomeValue merge;
	merge.hello = 60;
	cout << merge.world << ' ' << merge.uiuxp << '\n';
	merge.uiuxp = 174;
	cout << merge.hello << ' ' << merge.world << '\n';
	
	return 0;
}

輸出

60 60
       174 174

 

列舉(不得為數字開頭)

#include<iostream>
using namespace std;

enum Value
{
	apple,
	Ak47,
	o6719,
	dfj234nfmd,
};

int main()
{
	Value our;
	our = dfj234nfmd;
	switch(our)
	{
		case apple:
		case o6719:
		case Ak47:
			cout << "yummy!";
			break;
		case dfj234nfmd:
			cout << "What is this?!";
			break;
	}
	return 0;
}

輸出

What is this?!

 

重新定義變數名稱

#include<iostream>
#include<string>

using namespace std;

int main()
{
	typedef string ux;
	ux ui("user");
	cout << ui;
	
	return 0;
}

輸出

user

 

串接區域class

#include<iostream>

using namespace std;

int main()
{
	class vov
	{
	public:
		int *pa;
		struct vov *next;
	}begin, began, begun;
	
	begin.next = &began;
	began.next = &begun;
	
	begun.pa = new int;
	*begun.pa = 6185;
	cout << *begin.next->next->pa;
	
	delete begun.pa;
	return 0;
}

輸出

6105

 

讀取命令列指令

#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
    for(int a=1;argc>a;a++) cout << argv[a] << '\n';
    return 0;
}

輸入

./a.out commit something about this.
#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
    for(int a=1;argc>a;a++) cout << argv[a] << '\n';
    return 0;
}

輸出

commit
something
about
this.
END

你可能感興趣的文章

【Kali Linux】[history -c]如何清除歷史記錄 非bash環境會使history部分功能變的非法

【Stable Diffusion web UI】[AI 作畫](Linux)無 NVIDIA 顯卡之伺服器運行測試 凡事就是要試試看,才知道最後的結果

【ESXI & Oepnwrt】基本錯誤處理 Oepnwrt 為一個自由度超高的軟體,很多事情都能請其幫忙完成,但也因此很複雜,這邊簡單的介紹幾個錯誤的處理方式

【Wickct】如何製作一個簡單的動態響應頁面和傳遞參數至其他頁面 身為一個後端架構程式,這個功能當然是非常重要之一

【MySQL】每個類別中取前三高 一個由多層次所組成的SELECT述句

【Linux】[CentOS 8] How to update sudo instruction The last time I wanted to update the sudo command should be 2017, right? It's 2021 in a blink of an

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

高捷少女:購票大作戰① 「各位乘客,本班機即將降落,感謝各位乘客的搭乘……」隨著空中小姐的廣播音,那架飛機逐漸降落在地面,裡面的乘客們也紛紛開始整理自己的行李。 那個有著歐美人五官的少女抓緊包包,看著外面的小港機場,臉上緩緩

高捷少女:美麗島的守護者② 這是在亦晨離開前的下午拍的,當時亦晨在美麗島跟小穹艾米等人在美麗島散步,為離開前補充一些回憶,這時剛好經過的小雅被艾米抓過來,做為同樣是高捷新人的她倆一同拍了一張紀念照。說起來她跟亦晨並不太熟,不過也

【歌評】蓮台野夜行 - 幻想的永遠祭 蓮台野的探險雖然結束了,但是,誰知道是不是一段新的探險的序曲呢?

【想法】關於網路上的謾罵這檔事 網路是個自由的世界,每個人都享有平等的發言權,但是,請永遠記得,在網路上漫罵別人一定會留下證據的,就算之後刪文,你怎能確保對方沒有截圖存證?

艾米莉亞和高捷戀旅③ 「妳最好給我一個完整的理由,告訴我妳為什麼要這麼做。」艾米雙手叉腰,看著這位冒名參賽的後輩。「我會根據妳的說詞來判定妳違反規定的懲處。」