Is it legal to write to std::string?

后端 未结 5 945
孤街浪徒
孤街浪徒 2020-12-03 21:21

In std::string there are only const members to fetch the data like c_str(). However I can get a reference to the first element of the string via operator[] and

5条回答
  •  一向
    一向 (楼主)
    2020-12-03 22:06

    As it has been pointed-out, one can use strings in algorithms that use iterators; the same case can be implemented using std::transform Ex:- consider a string 's' to be converted to lower case:

    int (*pf)(int)=tolower; //lowercase
    std::transform(s.begin(), s.end(), s.begin(), pf); 
    

    Regards,

提交回复
热议问题