[精讚] [會員登入]
706

【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

你可能感興趣的文章

【Vim】解決貼上文字時出現過多空格的問題 換了新系統,有時候就算重裝軟體並且複製了設定檔也還是會出現非常奇怪的現象

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

【Wickct】(縮短網址) 如何將網頁掛載到特定路徑下 Wildfly的預設網址又臭又長又不好記,而且會被看到後端的目錄路徑安排,當然能藏就盡量藏起來啦

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

【無料版模】﹝CSS&HTML﹞製作epub電子書版模免費下載&附使用教學 一個專門用來製作epub的簡單CSS檔案,基本上已經可以做出一本還不錯的電子書,讓妳的電子書不在只有預設的字體、樣式

【C】(%c, %d)解決讀取字元時的緩衝區殘留 不解決就會莫名其妙地冒出一些莫名其妙的東西

隨機好文

高捷少女:小穹與果仁巧克力㊦ 「如果妳跟一個女生同班三年,看過她午休流口水跟狼吞虎嚥地吃午餐,就算變成高捷代言人,也很難把她當女神的啦!」她說,小穹氣得搶走她義大利麵裡的蝦子,其他人笑得花枝招展。

【歌評】蓮台野夜行 - 幻視之夜 ~ Ghostly Eyes 若有什麼是在聽到boss曲前的鋪襯,那一定就是每個系列的道中曲

【日翻中歌詞】LOSER 一如往常的孤身一人 早就已經被折磨殆盡 明明就已經無處可去 卻作著白日夢 晚安 無論何時都是這個樣子 對懵懂夜晚早感到噁心

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

【贈送序號】[一次性序號](先搶先贏)RO仙境傳說 因個人用不到,所以將其收集後放上來贈送給大家