string与char数组的转换

string转char数组

使用c_str函数

string a="hello world";
const char* ch=a.c_str();

注意: c_str函数的返回值是const char*的,不能直接赋值给char*,

###使用c_str和strcpy函数

string s("test string");
char str[100];
strcpy(str, s.c_str());

##char数组转string

###直接利用string的构造函数

char sz[]="awdasdas"
string str = sz;

#string与数字的转换

##string转数字

stoi

int stoi (const string&  str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);

该函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符。然后,从这个字符开始,根据依赖于基本参数的语法,获取尽可能多的有效字符,并将其解释为数值。最后,指向str中整数表示的第一个字符的指针存储在endptr指向的对象中。

idx:

指向类型为size_t的对象的指针,其值由函数设置为数值后的str中下一个字符的位置。

该参数也可以是一个空指针,在这种情况下,它不被使用。

base:

确定有效字符及其解释的数字基数(基数)。

如果为0,则使用的基数由序列中的格式决定(详见strtol)。请注意,默认情况下,此参数为10,而不是0。

如果base的值为零,那么预期的语法类似于整数常量的语法,它由以下连续形成:

  1. 可选的符号(+或-)

  2. 一个可选的前缀,表示八进制或十六进制基数(“0"或"0x”/“0X"分别)

  3. 一个十进制数字序列(如果没有指定基础前缀),或者如果存在特定的前缀,则为八进制或十六进制数字

如果基础值在2到36之间,则整数数字所期望的格式是代表指定基数(从第36个开始'0’到’z’/ ‘Z’之前)所需的任何有效数字和/或字母的连续。该序列可以可选地前面有一个符号(一个+或-),如果基数为16,则是可选的"0x"或"0X"前缀。

如果str中的第一个非空白字符序列不是上面定义的有效整数,或者如果没有这样的序列存在,因为str是空的或者只包含空白字符,则不执行转换。

举例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

int main ()
{
  std::string str_dec = "2001, A Space Odyssey";
  std::string str_hex = "40c3";
  std::string str_bin = "-10010110001";
  std::string str_auto = "0x7f";

  std::string::size_type sz;   // alias of size_t

  int i_dec = std::stoi (str_dec,&sz);
  int i_hex = std::stoi (str_hex,nullptr,16);
  int i_bin = std::stoi (str_bin,nullptr,2);
  int i_auto = std::stoi (str_auto,nullptr,0);

  std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
  std::cout << str_hex << ": " << i_hex << '\n';
  std::cout << str_bin << ": " << i_bin << '\n';
  std::cout << str_auto << ": " << i_auto << '\n';

  return 0;
}

stol

long stol (const string&  str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// stol example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stol

int main ()
{
  std::string str_dec = "1987520";
  std::string str_hex = "2f04e009";
  std::string str_bin = "-11101001100100111010";
  std::string str_auto = "0x7fffff";

  std::string::size_type sz;   // alias of size_t

  long li_dec = std::stol (str_dec,&sz);
  long li_hex = std::stol (str_hex,nullptr,16);
  long li_bin = std::stol (str_bin,nullptr,2);
  long li_auto = std::stol (str_auto,nullptr,0);

  std::cout << str_dec << ": " << li_dec << '\n';
  std::cout << str_hex << ": " << li_hex << '\n';
  std::cout << str_bin << ": " << li_bin << '\n';
  std::cout << str_auto << ": " << li_auto << '\n';

  return 0;
}

stoul

unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <iostream>   // std::cin, std::cout
#include <string>     // std::string, std::stoul, std::getline

int main ()
{
  std::string str;
  std::cout << "Enter an unsigned number: ";
  std::getline (std::cin,str);
  unsigned long ul = std::stoul (str,nullptr,0);
  std::cout << "You entered: " << ul << '\n';
  return 0;
}

###stoll

long long stoll (const string&  str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoll

int main ()
{
  std::string str = "8246821 0xffff 020";

  std::string::size_type sz = 0;   // alias of size_t

  while (!str.empty()) {
    long long ll = std::stoll (str,&sz,0);
    std::cout << str.substr(0,sz) << " interpreted as " << ll << '\n';
    str = str.substr(sz);
  }

  return 0;
}

###stoull

unsigned long long stoull (const string&  str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// stoull example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoull

int main ()
{
  std::string str = "8246821 0xffff 020 -1";

  std::string::size_type sz = 0;   // alias of size_t

  while (!str.empty()) {
    unsigned long long ull = std::stoull (str,&sz,0);
    std::cout << str.substr(0,sz) << " interpreted as " << ull << '\n';
    str = str.substr(sz);
  }

  return 0;
}

stof

float stof (const string&  str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// stof example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stof

int main ()
{
  std::string orbits ("686.97 365.24");
  std::string::size_type sz;     // alias of size_t

  float mars = std::stof (orbits,&sz);
  float earth = std::stof (orbits.substr(sz));
  std::cout << "One martian year takes " << (mars/earth) << " Earth years.\n";
  return 0;
}

###stod

double stod (const string&  str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// stod example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main ()
{
  std::string orbits ("365.24 29.53");
  std::string::size_type sz;     // alias of size_t

  double earth = std::stod (orbits,&sz);
  double moon = std::stod (orbits.substr(sz));
  std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
  return 0;
}

stold

long double stold (const string&  str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// stold example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main ()
{
  std::string orbits ("90613.305 365.24");
  std::string::size_type sz;     // alias of size_t

  long double pluto = std::stod (orbits,&sz);
  long double earth = std::stod (orbits.substr(sz));
  std::cout << "Pluto takes " << (pluto/earth) << " years to complete an orbit.\n";
  return 0;
}

##数字转string

1
2
3
4
5
6
7
8
9
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// to_string example
#include <iostream>   // std::cout
#include <string>     // std::string, std::to_string

int main ()
{
  std::string pi = "pi is " + std::to_string(3.1415926);
  std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
  std::cout << pi << '\n';
  std::cout << perfect << '\n';
  return 0;
}