[精讚] [會員登入]
699

【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

你可能感興趣的文章

【教程】[HTML](進階版)如何在手機上編輯電子書(epub)預覽介面 可能會有人認為電子書(epub)只能用電腦來開啟、編輯,其實不是的,手機也可以編輯喔!

【Discord bot】[botton]按鈕的使用、響應和關閉 Discord的botton通常都要和View配合使用。

【JDA/discord bot】刪除事件或slash(斜槓)指令的reply訊息 如何正確的等待 Async 的結束,在進行接下來的刪除訊息動作

【Wicket】[Cookie]如何讀取和設定客戶端的Cookie 對於某些參數需要給予使用者,我們可以使用 Cookie 來讓客戶端記著,之後再跟伺服器裡的比對來達到驗證的目的

【Javascript】(Event)常用的網頁事件 不寫下來絕對下又會忘記,然後每次又都要回到MDN去查,麻煩

【Firefox \ Maven】[Headless](Linux) 如何使用Maven打包並驅動Firefox 有時候我們需要取得渲染後的網頁,當然是直接呼叫瀏覽器出來用啦

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

【手遊介紹】小品手遊─寶箱是我的!(SUMMONER'S GREED) 輕鬆無腦的塔防遊戲,殺時間本小編推薦的遊戲之一

[活動] 2017年4/1雲空幻想愚人節活動彩蛋&攻略 (紀念性質) 雲空幻想2017年的愚人節活動介紹同時也是本小編的第一篇網路文章(*^ω^)♪

高捷少女:地下城的探險少女⑤ 小穹寫好後,耐耐看了一遍。「我想我應該辦得到。」她說完跪坐在地上,然後把古箏安放在大腿上,並將樂譜放在前面的地板。「要開始囉。」耐耐閉上眼睛,深呼吸一口氣,小穹等人在一旁看著她,心中暗自替她打氣。

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

高捷少女:美麗島的守護者④ 光之穹頂的某處垃圾桶底,一個四方形的機器持續發出聲音,機器的儀表板上顯示著「1:25:10」的字樣,外表被一層鞭炮所掩蓋。儀表板的數字每秒不斷減少,細微的嗶嗶聲也隨著數字的改變發出,但在熙來攘往的美麗