日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C/C++ 字符串(string)转换

發布時間:2024/2/28 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C/C++ 字符串(string)转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

C語言字符串與基本數據類型互轉

C++?string與基本數據類型互轉


前言

本篇博文介紹C語言和C++中字符串與基本數據類型的轉換問題,在這之前要先了解以下byte string和string的區別。在很多情況下,不管是C語言還是C++,都會將一個字符序列asdasd統稱為字符串。但是在外國網站查看一些函數后就會發現,C的函數參數是byte string(在剛開始看的時候我還在糾結這個字節字符串是什么東西?其實它就是字符數組或者const char *),直接翻譯過來是字節字符串(或字節串)。而C++的函數參數是string,它是一個string類的對象。如有需要可以將一個string對象的內容轉換成const char *。

?

C語言字符串與基本數據類型互轉

字符串(字符數組)轉為各種基本數據類型

Defined in header <stdlib.h> 字符串轉整型 int?? ? ? atoi(?const?char?*str?); long?? ? ?atol(?const?char?*str?); long?long?atoll(?const?char?*str?);字符串轉浮點型 double atof( const char* str );

#include <stdio.h> #include <stdlib.h>int main(void) {printf("%i\n", atoi(" -123junk"));printf("%i\n", atoi("0"));printf("%i\n", atoi("junk")); // no conversion can be performedprintf("%i\n", atoi("2147483648")); // UB: out of range of int } 輸出: -123 0 0 -2147483648 #include <stdlib.h> #include <stdio.h>int main(void) {printf("%g\n", atof(" -0.0000000123junk"));printf("%g\n", atof("0.012"));printf("%g\n", atof("15e16"));printf("%g\n", atof("-0x1afp-2"));printf("%g\n", atof("inF"));printf("%g\n", atof("Nan"));printf("%g\n", atof("1.0e+309")); // UB: out of range of doubleprintf("%g\n", atof("0.0"));printf("%g\n", atof("junk")); // no conversion can be performed } 輸出: -1.23e-08 0.012 1.5e+17 -107.75 inf nan inf 0 0

?

各種基本數據類型轉為字符串(字符數組)

(1)sprintf(buf, "%d" , value); 用于轉換帶符號的整數 (2)sprintf(buf, "%ld", value); 用于轉換帶符號的整數 (3)sprintf(buf, "%lld",value); 用于轉換帶符號的整數 (4)sprintf(buf, "%u", value); 用于轉換無符號整數 (5)sprintf(buf, "%lu", value); 用于轉換無符號整數 (6)sprintf(buf, "%llu",value); 用于轉換無符號整數 (7)sprintf(buf, "%f", value); 用于轉換float、double (8)sprintf(buf, "%Lf", value); 用于轉換long double #include <stdlib.h> #include <stdio.h>int main(void) {char buf[100];sprintf(buf, "%d", -12); printf("%c%c%c\n", buf[0], buf[1], buf[2]);sprintf(buf, "%ld", 12); printf("%s\n", buf); sprintf(buf, "%lld", 122222222222222); printf("%s\n", buf); sprintf(buf, "%u", 1);printf("%s\n", buf); sprintf(buf, "%lu", 111111111111); printf("%s\n", buf); sprintf(buf, "%llu", 2e18);printf("%s\n", buf); sprintf(buf, "%f", 12.8888);printf("%s\n", buf); sprintf(buf, "%Lf", 1.222e8);printf("%s\n", buf); } 輸出: -12 12 122222222222222 1 111111111111 4880707296814876672 12.888800 0.000000

?

將字符串轉換為整數值

Defined in header <stdlib.h> <cstdlib> long?? ? ?strtol(?const?char?*str,?char?**str_end,?int?base?); long?long?strtoll(?const?char?*str,?char?**str_end,?int?base?);

str

-

pointer to the null-terminated byte string to be interpreted

str_end

-

pointer to a pointer to character.

base

-

base?of the interpreted integer value

#include <iostream> #include <string> #include <cerrno> #include <cstdlib> #include <cstdio> int main() {const char* p = "10 200000000000000000000000000000 30 -40";char *end;std::cout?<< "Parsing '" << p << "':\n";for (long i = std::strtol(p, &end, 10);p != end;i = std::strtol(p, &end, 10)){std::cout?<< "'" << std::string(p, end-p) << "' -> ";p = end;if (errno == ERANGE){std::cout?<< "range error, got ";errno = 0;}std::cout?<< i << '\n';}// parsing without error handlingprintf("\"1010\" in binary --> %ld\n", strtol("1010",NULL,2));printf("\"12\" in octal --> %ld\n", strtol("12",NULL,8));printf("\"A\" in hex --> %ld\n", strtol("A",NULL,16));printf("\"junk\" in base-36 --> %ld\n", strtol("junk",NULL,36));printf("\"012\" in auto-detected base --> %ld\n", strtol("012",NULL,0));printf("\"0xA\" in auto-detected base --> %ld\n", strtol("0xA",NULL,0));printf("\"junk\" in auto-detected base --> %ld\n", strtol("junk",NULL,0)); }

?

將字符串轉換為浮點型

Defined in header <stdlib.h> <cstdlib> float?? ? ? strtof(?const?char*?str,?char**?str_end?); double?? ? ?strtod(?const?char*?str,?char**?str_end?); long?double?strtold(?const?char*?str,?char**?str_end?);

str

-

pointer to the null-terminated byte string to be interpreted

str_end

-

pointer to a pointer to character.

#include <iostream> #include <string> #include <cerrno> #include <cstdlib> int main() {const char* p = "111.11 -2.22 0X1.BC70A3D70A3D7P+6 ?1.18973e+4932zzz";char* end;std::cout?<< "Parsing \"" << p << "\":\n";for (double f = std::strtod(p, &end); p != end; f = std::strtod(p, &end)){std::cout?<< "'" << std::string(p, end-p) << "' -> ";p = end;if (errno == ERANGE){std::cout?<< "range error, got ";errno = 0;}std::cout?<< f << '\n';} }

?

C++?string與基本數據類型互轉

string轉為整數

int?? ? ? stoi(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); int?? ? ? stoi(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?); long?? ? ?stol(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); long?? ? ?stol(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?); long?long?stoll(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); long?long?stoll(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?);

str

-

the string to convert

pos

-

address of an integer to store the number of characters processed

base

-

the number base

#include <iostream> #include <string> int main(){std::string?str1 = "45";std::string?str2 = "3.14159";std::string?str3 = "31337 with words";std::string?str4 = "words and 2";int myint1 = std::stoi(str1);int myint2 = std::stoi(str2);int myint3 = std::stoi(str3);// error: 'std::invalid_argument'// int myint4 = std::stoi(str4);std::cout?<< "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';std::cout?<< "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';std::cout?<< "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';//std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n'; }

?

string轉為無符號long

unsigned?long?? ? stoul(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); unsigned?long?? ? stoul(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?); unsigned?long?long?stoull(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); unsigned?long?long?stoull(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?);

str

-

the string to convert

pos

-

address of an integer to store the number of characters processed

base

-

the number base

?

string轉為浮點型

float?? ? ? stof(?const?std::string&?str,?std::size_t*?pos?=?0?); float?? ? ? stof(?const?std::wstring&?str,?std::size_t*?pos?=?0?); double?? ? ?stod(?const?std::string&?str,?std::size_t*?pos?=?0?); double?? ? ?stod(?const?std::wstring&?str,?std::size_t*?pos?=?0?); long?double?stold(?const?std::string&?str,?std::size_t*?pos?=?0?); long?double?stold(?const?std::wstring&?str,?std::size_t*?pos?=?0?);

str

-

the string to convert

pos

-

address of an integer to store the number of characters processed

?

各類型轉string

Defined in header?<string> std::string?to_string(?int?value?); std::string?to_string(?long?value?); std::string?to_string(?long?long?value?); std::string?to_string(?unsigned?value?); std::string?to_string(?unsigned?long?value?); std::string?to_string(?unsigned?long?long?value?); std::string?to_string(?float?value?); std::string?to_string(?double?value?); std::string?to_string(?long?double?value?);

#include <iostream> #include <string> int main() {double f = 23.43;double f2 = 1e-9;double f3 = 1e40;double f4 = 1e-40;double f5 = 123456789;std::string?f_str = std::to_string(f);std::string?f_str2 = std::to_string(f2); // Note: returns "0.000000"std::string?f_str3 = std::to_string(f3); // Note: Does not return "1e+40".std::string?f_str4 = std::to_string(f4); // Note: returns "0.000000"std::string?f_str5 = std::to_string(f5);std::cout?<< "std::cout: " << f << '\n'<< "to_string: " << f_str ?<< "\n\n"<< "std::cout: " << f2 << '\n'<< "to_string: " << f_str2 << "\n\n"<< "std::cout: " << f3 << '\n'<< "to_string: " << f_str3 << "\n\n"<< "std::cout: " << f4 << '\n'<< "to_string: " << f_str4 << "\n\n"<< "std::cout: " << f5 << '\n'<< "to_string: " << f_str5 << '\n'; }

一個非常好的網站:?http://en.cppreference.com/w/cpp/string/basic_string/stol

總結

以上是生活随笔為你收集整理的C/C++ 字符串(string)转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。