[精讚] [會員登入]
695

【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

你可能感興趣的文章

【Discord bot 2.0.1】(discord.ui.View)如何將舊機器人升級至目前最新版本 很多時候升級軟體是為了讓別人覺得你的程式很先進,但這次卻是因為要使用某個新功能

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

【Maven】如何創建一個簡單可部屬的WAR檔 滿重要的一個大功能,在使用JAVA網頁伺服器的時候一定會需要這個WAR檔來進行部屬

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

【Linux】網路測速 network speed test 在沒有 GUI 的狀態之下,該如何進行簡單的網路測速呢?

【JDA/discord bot】package does not exist fix JDA 4.0 和 5.0 差別還是很大的

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

高捷少女:購票大作戰② 一個不好的預感浮現,艾米莉亞開始檢查屋子四處。窗戶跟陽台都有關好,也沒有被打開的跡象。但一股無形的壓力,開始在寂靜的公寓中蔓延,她不安地嚥一下喉嚨。最後,她走向那扇窗戶,那前天晚上,白龍為了逃脫,而撞

高捷少女:美麗島的守護者③ 小雅閉上眼睛,思索在高捷發生的點點滴滴。她心意已決,在高捷的日子的確也有快樂的部分,不過她相信換個方向是更好的決定。有關高捷的所有美好回憶,小雅決定保留在心裡就好,繼續在高捷工作只會讓自己更痛苦而已,

高捷少女:耐耐的新年驚喜② 「各位,我跟爸媽聊完了……」耐耐走進客廳,看到大家在看自己的相簿,臉蛋立刻紅了。「哇啊啊,不要看那個!」她三步併作兩步地走向少女們,將相簿拿走。「為什麼啊?小時候的耐耐很可愛啊。」婕兒不解地說。

【數學】徐氏數學簡明講義(三) 第二章 直線與園 P2.1-17 Q26 26.三角形的兩邊分別在二直線 x-3y+10=0 , 2x-y-8=0 上,且知第三邊中點為(3 , 2),求第三邊所

【英翻中歌詞】(二創歌)少女秘封俱樂部 Out there past the stars... that world is ours, once lost to history.  (Don’t you know?~) 在星輝之外...那屬