首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C++ 字串型別轉換完全手冊:std::string、CString、char*、int 互轉

     編輯:關於C語言

核心標準庫頭文件

#include <string>     // std::string
#include <sstream>   // std::stringstream(推薦轉換工具)
#include <cstdlib>   // atoi、strtol 等 C 風格函式
// MFC/ATL 專用
#include <atlstr.h>  // CString

高頻轉換速查表

來源 → 目標推薦寫法備註
int → std::stringstd::to_string(n) (C++11) 或 stringstream最簡潔、無 locale 問題
std::string → intstd::stoi(s) / std::stol / std::stoll拋出 std::invalid_argument / out_of_range
std::string → CStringCString(str.c_str()) 或 CString(str.c_str(), str.length())構造函式直接接受 const char*
CString → std::stringstd::string(cstr.GetString()) (MFC) / std::string(CW2A(cstr)) (ATL Unicode)避免 GetBuffer() 配對 ReleaseBuffer() 易忘
char* → std::stringstd::string(ptr) 或 std::string(ptr, len)若含 \0 中間截斷,必須指定長度
std::string → char*str.c_str() (唯讀) / str.data() (C++11 保證連續)返回指標生命週期綁定 str,勿儲存長期使用
CString ↔ intCString s; s.Format(_T("%d", n)); int n = _ttoi(s);_ttoi 自動適配 Unicode/ANSI
CString ↔ char*CString s = psz; char* p = s.GetBuffer(); ... s.ReleaseBuffer();修改緩衝區後必須 ReleaseBuffer

深度解析:三大關鍵函式

c_str() vs data() vs copy()

  • c_str():返回以 \0 結尾的常量指標,C++11 保證 O(1)、不拋異常。
  • data():C++11 前不保證尾隨 \0;C++11 起等同 c_str()。
  • copy(buf, len):將內容複製到使用者提供的緩衝區,安全可控,適合與 C API 交互。

進階技巧:自定義格式化轉換

// 任意型別 → string (支援 iomanip 格式控制)
template <typename T>
std::string to_string_fmt(T value, const char* fmt = "%s") {
    std::ostringstream oss;
    oss << value; // 或用 snprintf 配合 fmt
    return oss.str();
}

// 安全的 string → 數值(含錯誤碼)
bool try_parse_int(const std::string& s, int& out) {
    try {
        size_t pos;
        out = std::stoi(s, &pos);
        return pos == s.size(); // 整串皆為數字
    } catch (...) { return false; }
}

避坑指南

  1. 編碼陷阱:MFC CString 在 Unicode 建置下存 wchar_t,轉 std::string 需經 CW2A/CT2A;反之亦然。
  2. 緩衝區生命週期:c_str() 返回指標隨 std::string 修改/銷毀失效,切勿 const char* p = str.c_str(); str.clear(); use(p);。
  3. GetBuffer/ReleaseBuffer 配對:漏呼 ReleaseBuffer 導致記憶體洩漏或長度錯誤。
  4. 舊式 atoi 無錯誤報告:atoi("abc") 返回 0 無法區分「真 0」與「解析失敗」,改用 strtol 或 std::stoi。

總結

現代 C++ 首選 std::to_string/std::stoi、stringstream、c_str() 三板斧;MFC/ATL 專案善用 CString 構造函式與 Format,配合 CT2A/CW2A 處理編碼。建立統一工具類 StringUtil 封裝上述邏輯,專案中統一調用,徹底告別轉換混亂。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved