[精讚] [會員登入]
358

【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

你可能感興趣的文章

【C++】一些好用的C++小功能 —— 貳 承襲上一篇的C++小功能,筆記下來以免自己以後忘記了。

【Maven / Jython】建立可直接執行的JAR檔案 以小編的前一篇文章為基礎,加了一些新東西進去,就變成另一個樣貌了

【Raspberry Pi/樹梅派】(gcc 10) 如何安裝 gcc & g++ 需要的時間非常久,不愧是gcc的編譯

【JDA/discord bot】取得頻道第一筆或最新(最後一筆)的歷史訊息 在不處理訊息的先後順序下取得相關的歷史訊息

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

【Maven】用Maven來託管Wildfly/jBoss的部屬(deploy)、解部屬(undeploy) 極簡版,給未來想要抄作業的小編自己,完整的一切設定檔,可以依照個人需求作增刪。

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

高捷少女:小穹與果仁巧克力㊥ 艾咪臉上泛起笑容說:「妳知道嗎?就跟花語一樣,每一種巧克力也都有各自的涵義:薄荷巧克力代表初戀;卡通巧克力代表天真爛漫;而果仁巧克力代表的是窩心,還有想陪伴對方的心情,這在德國是女生之間在慶生時會彼此

高捷少女:美麗島的守護者⑥ 一陣貓叫傳到小雅耳中,原本要朝小雅撲過去的北風轉了個圈,從半空中落地,牠的表情宛如五雷轟頂。這聲音……難道是……

【阿瑞斯病毒】(影片示範)快速採滿100塊的鐵礦一點都不難! 感覺超像廢文的一篇?連小編自己都覺得寫得很差勁,還是上一篇擊殺棘刺豬教學的那篇寫得比較好!

【英翻中歌詞】(二創歌)魔術師梅莉 "Darling, you're dreaming." 「親愛的,你正在作夢」 Waking or sleeping means nothing to me. 現實與夢境只有一線之隔 If you

【英翻中歌詞】(二創歌)Pure Furies ~ 心之所在 the time has come 時辰已到 after all that pain my thirst for ven