C++ 字符串库函数整理

来源:互联网 发布:xp动态壁纸软件 编辑:程序博客网 时间:2024/05/22 09:48
<span style="font-size:18px;">#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sstream>using namespace std;int sToI(string s){                      //字符串转为 int    int myInt;    istringstream buffer(s);     buffer>>myInt;    return myInt;}string iToS(int myInt){                  //int 转为字符串    ostringstream buffer;    buffer<<myInt;    return buffer.str();}void PrintStr(const char* str) {    cout<<str<<endl;}//将字符串翻转void PrintRevStr(char* str) {    cout<<strrev(str)<<endl;}int main() {    //字符串转化为数字,需要stdlib.h    char a[] = "-100.23";    char b[] = "200.00";    float c = atof(a) + atof(b);    cout<<c<<endl;    //数字转化为字符串    char buffer[50];    int int_1 = 10020030;    //sprintf,需要stdio.h    sprintf(buffer, "%d", int_1);    cout<<buffer<<endl;    //itoa,注意:两种方法对buffer的影响不叠加    itoa(int_1, buffer, 10);    cout<<buffer<<endl;    //string转化为const char*    string str_1 = "abcde";    const char* char_1 = str_1.c_str();    PrintStr(char_1);    char str_2[] = "abcdefg";    PrintRevStr(str_2);    char s[] = "abcde";    strrev(s);    cout<<s<<endl;}</span>

0 0
原创粉丝点击