C++中的字符串的对比:char* std:string char[]

3 篇文章 0 订阅

char*

语法:

char* str = "This is GeeksForGeeks";
好处
  1. 只需要一个指针 来引用str,内存上更高效
  2. 不需提前声明str size
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    // pointer str points to const string literal "Hello". 
    // No need to declare size. 
    char* str1 = "This is GeeksForGeeks"; 
  
    cout << str1 << endl; 
  
    int size = 30; 
  
    // can allocate size dynamically. 
    char* str2 = (char*)malloc(sizeof(char) * size); 
  
    str2 = "GeeksForGeeks For Everyone"; 
  
    cout << str2; 
  
    return 0; 
} 

弊端
  1. char在C中很好,但C++不推荐。C++中编译器warning“deprecated conversion from string constant to ‘char’”。因为在C中string是char的队列,但在C++中是constant array of char(char常亮的队列),所以使用const char*
    const char* str = "This is GeeksForGeeks";
  2. 不能更改字符串的内容,但可以更改指针的指向
// CPP program to illustrate assigning 
// *char value to other variable  
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    // This initialization gives warning in C++. 
    // "deprecated conversion from string constant 
    // to 'char*'" 
    char* str = "Hello"; 
  
    const char* str1 = "Hello"; // No warning 
  
    // trying to modify const string literal 
    // gives Runtime error 
    str[1] = 'o'; 
  
    cout << str << endl; 
  
    return 0; 
} 

std::string

语法
std::string str = "This is GeeksForGeeks";

str是 std::string类的对象, std::string是basic_string类模板的实例(模板使用char作为它的character type)。

当你用std::string时,不要使用cstring、string.h的方法。因为std::string是basic_string类型,cstring是const char*类型

好处

string有很多的方法,替换、查询等

// CPP program to illustrate  
// std::string functions 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    // string assignment 
    string s1 = "Hello"; 
    string s2 = "World"; 
  
    // return length of string 
    cout << s1.size() << endl; // 5 
    cout << s2.length() << endl; // 5 
  
    // concatenate string using + operator. 
    s1 = s1 + s2; 
    cout << s1 << endl; // HelloWorld 
  
    // append string 
    s1.append("Geeks"); 
    cout << s1 << endl; // HelloWorldGeeks 
  
    string s3 = "HelloWorldGeeks"; 
  
    // compare two strings 
    if (s1.compare(s3) == 0) 
        cout << "true" << endl; 
    else
        cout << "false" << endl; 
  
    // substring of string s1 
    // substr(pos, length_of_substring) 
    string sub = s1.substr(0, 5); 
    cout << sub << endl; // Hello 
  
    // insert into string 
    // insert(pos, string) 
    s1.insert(10, "For"); 
    cout << s1 << endl; // HelloWorldForGeeks 
  
    string target = "World"; 
  
    // find a target string in s1 
    size_t pos = s1.find(target); 
    if (pos != std::string::npos) // npos=-1 
        cout << "Found at Position:" << pos << endl; // pos=5 
  
    // replace a portion of string s1 
    // replace(pos, length_of_portion, string_to_replace) 
    cout << s1.replace(5, 5, "Geeks") << endl; // HelloGeeksForGeeks 
  
    return 0; 
} 
char* 优于std:string的情况
  1. 和底层os交互时
  2. 兼容老的c代码(不过 std::string的 c_str() 方法处理了 很大一部分该问题)
  3. 节省内存
std:string to const char
        std::string message = "index:" + std::to_string(index);
        char *messageChar = new char[message.size() + 1];
        std::strncpy(messageChar, message.c_str(), message.size());

        const char *finalMessage = &messageChar[0];

        delete[] messageChar;

char[]

语法

char str[] = "This is GeeksForGeeks";
     or 
char str[size] = "This is GeeksForGeeks";
//  Here str is a array of characters denoting the string.
好处

可以动态修改str内容

坏处
  1. 实在stack上分配了固定大小的内存空间
  2. 因size固定,如果想拼接等操作str,需要申请较大的空间。对于此,可以用C++标准库cstring和string.h处理
// CPP program to illustrate char 
// concatenation using standard functions 
#include <iostream> 
#include <cstring> 
using namespace std; 
  
int main() 
{ 
    // take large size of array 
    char str[10] = "Hello"; 
  
    cout << "Before Concatenation : " << str << endl; // Hello 
    strcat(str, " World"); 
    cout << "After Concatenation : " << str; // Hello World 
  
    return 0; 
} 
cstring常用方法
#include <iostream> 
#include <cstring> 
using namespace std; 
  
int main() 
{ 
    char s1[10] = "Hello"; 
  
    // return length of s1 
    cout << strlen(s1) << endl; 
  
    char s2[50]; 
  
    // copies s1 into s2 
    strcpy(s2, s1); 
    cout << s2 << endl; 
  
    char s3[10] = "World"; 
  
    // concatenates s3 into s2 
    strcat(s2, s3); 
    cout << s2 << endl; 
  
    char s4[50] = "HelloWorld"; 
  
    // return 0 if s2 and s4 are equal. 
    if (strcmp(s2, s4) == 0) 
        cout << "true" << endl; 
    else
        cout << "false" << endl; 
  
    char s5[30]; 
  
    // copies first 5 chars of s2 into s1 
    strncpy(s5, s4, 5); 
    cout << s5 << endl; 
  
    char target[10] = "Hello"; 
  
    // search for target string in s4 
    if (strstr(s4, target) != NULL) 
        cout << "true" << endl; 
    else
        cout << "false" << endl; 
  
    return 0; 
} 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值