分類

展開全部 | 收合全部

分類

展開全部 | 收合全部

【C++ 基礎語法 #2】C/C++ 顯示資料的類別 (type) sample code (內含範例程式碼) print C data type, cout C++ data type, get variable type in c++

前言

我們很常會想要知道一個 C++ 變數的 資料型態 (type),
但 C++ 難道沒有像 python 一樣直接 type() 就能直接看出資料型態 (type) 的 function 嗎?
雖然有點不同,但其實還是有的!!!

我覺得這篇應該會有很多人有跟我一樣的問題!!

使用方法 – 主要使用 typeid 相關的功能

typeid(T).name()

範例程式碼

#include <iostream>
using namespace std;

int main()
{
    const char* s = "Hello";
    cout << typeid(s).name() << endl;

    return 0;
}

編譯與結果

> g++ test.cpp  -std=c++11 -o a.out &&  ./a.out
PKc

「PKc」就是我們得到的結果,但我們看不懂啊!!!似乎我們還要想辦法去翻譯一下?

註:

我們印出來的是 g++ decorated name,也就是說我們需要去解構 (demangle) 他,詳細可參考下方連結。
What you get from g++ is a decorated name, that you can “demangle” using the c++filt command or __cxa_demangle.
參考此文:https://stackoverflow.com/questions/4465872/why-does-typeid-name-return-weird-characters-using-gcc-and-how-to-make-it-prin

整理格式

依照上方連結的作法,我們在編譯時稍微修改一下 flag,

編譯與結果

❯ g++ datatype_test.cpp  -std=c++11 -o a.out &&  ./a.out | c++filt --types;
char const*

「char const*」就是我們得到的結果,我們終於看得懂啦!!!

Playground – 觀察 char array, char*, string, stringstream 之間的關係 (可觀察變化學習),後續會有文章專門討論

範例程式碼

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
  int a = 2;
  float b = 3.0;
  char s0[10] = "Hello";
  char s1[] = "Hello";
    const char* s2 = "Hello";
  string s3 = "Hello";
  stringstream s4;

  cout << typeid(a).name() << ", " << a << endl;
  cout << typeid(b).name() << ", " << b << endl;

  cout << " ------ s0 testing ------ " << endl;
  cout << typeid(s0).name() << ", " << s0 << endl;
  cout << " ------ s1 testing ------ " << endl;
  cout << typeid(s1).name() << ", " << s1 << endl;
  cout << " ------ s2 testing ------ " << endl;
  cout << typeid(s2).name() << ", " << s2 << endl;
  cout << " ------ s3 testing ------ " << endl;
  cout << typeid(s3).name() << ", " << s3 << endl;
  cout << " ------ s4 testing ------ " << endl;
  cout << typeid(s4).name() << endl;
  s4 << s3;
  cout << typeid(s4).name() << endl;
  cout << typeid(s4.str()).name() << ", " << s4.str() << endl;
  cout << typeid(s4.str().c_str()).name() << ", " << s4.str().c_str() << endl;

    return 0;
}

編譯與結果

> g++ datatype_test.cpp  -std=c++11 -o a.out &&  ./a.out | c++filt --types;
int, 2
float, 3
 ------ s0 testing ------
char [10], Hello
 ------ s1 testing ------
char [6], Hello
 ------ s2 testing ------
char const*, Hello
 ------ s3 testing ------ 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Hello
char const*, Hello
 ------ s4 testing ------
std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >
std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Hello
char const*, Hello

Refernce

https://stackoverflow.com/questions/4465872/why-does-typeid-name-return-weird-characters-using-gcc-and-how-to-make-it-prin
https://docs.microsoft.com/zh-tw/cpp/cpp/typeid-operator?view=msvc-160
https://en.cppreference.com/w/cpp/language/typeid
https://iter01.com/553394.html
https://github.com/willwray/type_name#HH
https://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c

⭐C++ 基礎用法 相關文章整理⭐:
1.【C++】C++ compile 程式碼 使用 c++ 11 與使用相關的 package
2.【C++】C/C++ 顯示資料的類別 (type) sample code (內含範例程式碼) print C data type, cout C++ data type, get variable type in c++
3.【C++】C++ 複製 2D array的方法 copy 2d array memcpy sample code (內含範例程式碼)
⭐Modern C++ ⭐:
⭐C++ 字串處理相關文章整理⭐:
1.【C++】字串 char string stringstream 相關用法總整理 (內含範例程式碼) 與利用 sprinf, snprinf assign 值的方法
2.【C++】字串 char string stringstream 「轉換」用法總整理 (內含範例程式碼)
3.【C】printf, fprintf, sprintf, snprintf 相關用法總整理 (內含範例程式碼)
4.【C++】C++ String 用法 連接兩個 String c++ string concat
⭐C++ 系統偵測相關文章整理⭐:
1.【C++】C++ 利用 dirent.h 計算資料夾的檔案數量 count files sample code (內含範例程式碼)
2.【C++】C++ inotify sample code 偵測指定路徑底下的文件變化 (內有範例程式碼)
⭐C++ OpenCV 相關文章整理⭐:
1.【OpenCV】c++ OpenCV – 在 ubuntu 上第一次執行 OpenCV 程式 sample code (內含範例程式碼)
2.【OpenCV】c++ OpenCV - 在圖片上寫上文字 cv::putText sample code (內含範例程式碼)
3.【OpenCV】c++ OpenCV - cv::Rect 矩形用法與相關功能函數 sample code (內含範例程式碼)
4.【OpenCV】c++ OpenCV - OpenCV 中的純量 定義顏色 cv::Scalar(255,255,255) color sample code (內含範例程式碼)
5.【OpenCV】用 C++ 計算 iou 的方法 與網路算法常見錯誤(內附範例程式碼) sample code
⭐C++ Makefile 相關文章整理⭐:
1.【C++ Makefile】- 1 / 嘗試撰寫自己的第一份 Makefile
2.【C++ Makefile】- 2 / 新增自己的變數
3.【C++ Makefile】- 3 / Makefile 常用變數 -「$@」, 「$^」
4.【C++ Makefile】- 4 / Makefile 常用 fake target -「.PHONY」
5.【C++ Makefile】- 5 / Makefile 內建變數 -「$@」, 「$^」, 「$<」, 「$* 」,「$? 」
⭐【喜歡我的文章嗎? 歡迎幫我按讚~ 讓基金會請創作者喝一杯咖啡!
如果喜歡我的文章,請幫我在下方【按五下Like】 (Google, Facebook 免註冊),會由 「LikeCoin」 贊助作者鼓勵繼續創作,讀者們「只需幫忙按讚,完全不用出錢」哦!

likecoin-steps
Howard Weng
Howard Weng

我是 Howard Weng,很多人叫我嗡嗡。這個網站放了我的各種筆記。希望這些筆記也能順便幫助到有需要的人們!如果文章有幫助到你的話,歡迎幫我點讚哦!
另外,因為定位是「個人的隨手筆記」,有些文章內容「⚠️可能我理解有誤⚠️」或「?只寫到一半?」,如果有發現這樣的情況,歡迎在該文章的最下面留言提醒我!我會儘快修正或補上!感謝大家的建議與幫忙,讓網站能變得更好?

文章: 890

★留個言吧!內容有誤或想要補充也歡迎與我討論!