string str字符串和char str[] 字符串数组

string类包含char型数组,char型数组想用string类型的字符串的话,要转换才行

string只能拼接字符串,不可以带int类型的数据,但是sprintf()可以

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name = "fan";
    string phone = "152462";
    string table = "test";
    string s;
    const char * sql;

    s = "INSERT INTO "+table+"(name, tele) values('"+name+"','"+phone+"');";
    cout << s << endl;

    sql = s.c_str();
    cout << sql <<endl;

    sql = s.data();
    cout << sql <<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/fanx021/article/details/80189078