1. 程式人生 > >C++ bool和string轉換

C++ bool和string轉換

直接貼程式碼吧,用g++可以編譯,測試ok

#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char **argv)
{
    bool a = true;

    ostringstream os1;
    os1 << a;
    cout << string(os1.str()) << endl;

    ostringstream os2;
    a = false;
    os2 << a;
    cout << string(os2.str()) << endl;

    stringstream ss1;
    ss1 << true;
    cout << ss1.str() << endl;

    stringstream ss2;
    ss2 << false;
    cout << ss2.str() << endl;

    bool b;  
    string s = "true";  
    istringstream(s) >> boolalpha >> b;
    cout << "b = " << b << endl;

    s = "false";
    istringstream(s) >> boolalpha >> b;
    cout << "b = " << b << endl;
    
    return 0;
}
編譯執行如下:



相關推薦

C++ boolstring轉換

直接貼程式碼吧,用g++可以編譯,測試ok #include <iostream> #include <sstream> using namespace std; int

InputStream只能讀取一次的解決辦法 C# byte[] Stream轉換

eof nbsp pos 讀取 處理 搜索 post ... 還要 x 情景--->>> 導入文件的時候,前臺傳過來一個文件, 後臺接到: HttpPostedFileBase file = Request.Files[0];由於對這個文件後臺處理

C++ char string 簡單用法

直接上程式碼,編譯執行,跟著結果對比就好理解 #include<iostream> #include<string> #include<sstream> #include<cstring> #include <stdlib.h>

C# List string 的互轉(以逗號分隔)

List轉字串,用逗號隔開 List<string> list = new List<string>(); list.Add("a"); list.Add("b"); list.Add("c"); //MessageBox.Show(list.);

Liststring[]轉換

System.String[] str={"str","string","abc"}; List<System.String> listS=new List<System.String>(str); System.String[] str=(new List<

C#實戰小技巧(九):List<string>string[]的相互轉換

List是string型別列表,string[]是string型別陣列,二者可以互相轉換。 1.string[]轉List string[] strArray = {"a", "ab", "abc"}; List<string> strList = new List<s

c#中 intString互相轉換

1,int轉成string用toString 或者Convert.toString()如下 例如:int varInt = 1; string varString = Convert.ToString(varInt); string varString2 = varInt.

C++ / Java】char陣列string的相互轉換及自動轉換

一般的轉換: #include<cstdio> #include<cstring> #include<string> using namespace std; char str[100]; string s; int main()

C# 4】文字轉換。串列埠通訊中字串string位元組陣列byte[]、ASCII的轉換

string轉byte[]:byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );byte[]轉string:string str = System.Text.Encoding.Default.GetString ( byteArra

C++中實現從std::string型別到bool型的轉換

利用輸入字串流:std::istringstream   ? 1 2 3 boolb; std::string s = "true"; std::istringstream(s) >> std::boolalpha >>

C++實現CStringstring的互相轉換

備忘:為了適用於Unicode環境,要養成使用_T()巨集的習慣 1、格式化字串 CString s; s.Format(_T("The num is %d."), i); 2、轉為 int 轉10進位制最好用_ttoi(),它在 ANSI 編碼系統中被編譯成_atoi(),而在 Unicode 編碼系統

C# byte[]型別String型別相互轉換

String型別轉成byte[]: byte[] byteArray = System.Text.Encoding.UTF8.GetBytes ( strvalue ); byte[]轉成String: String str = System.Text.Encoding.U

C# DataTable List之間相互轉換的方法

dbn execute 屬性 ins 集合 方法 summary efault getprop 一、List<T>/IEnumerable轉換到DataTable/DataView private DataTable ToDataTable<T>(

c/c++日期時間處理與字符串string轉換

輸入輸出 一個數 代碼 string轉換 計算 字符 span 日期 時間轉化 在c/c++實際問題的編程中,我們經常會用到日期與時間的格式,在算法運行中,通常將時間轉化為int來進行計算,而處理輸入輸出的時候,日期時間的格式卻是五花八門,以各種標點空格相連或者不加標點。

c#基礎】int 轉換 stringstring 轉換 int

方法 res data int 是否 tryparse php convert out 1、int 轉換 string方法:toString() 或者 Convert.toString()舉例: [code]phpcode://toString() int a =1; st

Javascript ArrayString的互轉換

參數傳遞 如果 red array類 gre blue bsp new val Array類可以如下定義: var aValues = new Array(); 如果預先知道數組的長度,可以用參數傳遞長度 var aValues = new

CStringstring在unicode與非unicode下的相互轉換(轉)

cst toc ref 編譯 end con adding eas font 原文轉自 http://blog.csdn.net/u014303844/article/details/51397556 CString和string在unicode與非unicode下的相

C++ int與string的相互轉換

strings 構造函數 size 三種 浮點型 cout int 成員 文件 一、int轉換成string   Ⅰ、to_string函數 c++11標準增加了全局函數std::to_string: string to_string (int val); string t

Java之byte、charString類型相互轉換

rac static rgs div body valueof () print byte[] 1 package basictype; 2 3 /** 4 * byte、char和String類型相互轉換 5 */ 6 public class C

基本數據類型String相互轉換

相互 color 結果 val ring pre integer args static 1 package com.jdk7.chapter5; 2 3 public class BasicToString { 4 /** 5 * 基本數據