Uh... std::string are very much mutable.
The following member functions can modify, directly or indirectly, the contents of an std::string object:
std::string::operator=()
std::string::push_back()
std::string::erase()
std::string::operator[]() (non-const version)
std::string::assign()
std::string::clear()
std::string::insert()
std::string::operator+=()
std::string::append()
std::string::replace()
Aren't strings really just "const char*", but in a class that provides functionality that is easier to use than just using const char* in your code?
I know for certain that in Java, the String class is immutable - it's methods do NOT change the contents of the object, rather produce another object with whatever changes are implemented in the method. I understand C++ != Java, but for some reason I remember being told (or read somewhere) that the C++ std::string class was immutable as well.
I don't like disagreeing with someone as reputable as helios, who is obviously more qualified than I, but I still don't understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string firstStr = "This is first string";
string secondStr = "This is second string";
secondStr[3] = "a";
cout << secondStr;
return 0;
}
Compile error: error C2440: '=' : cannot convert from 'const char [2]' to 'char'
helios is correct. std::string is mutable. He already posted many functions that alter the string contents.
Aren't strings really just "const char*", but in a class that provides functionality that is easier to use than just using const char* in your code?
No. std::string allocates and maintains a buffer and the contained string, as well as other bits of information like the length, allocated space, etc.
const char* is just a pointer that probably points to a c-style string (but might not)
1 2 3
string secondStr = "This is second string";
secondStr[3] = "a";
This code is wrong. By using the [] operator you're trying to access a single chararacter in the string, but you're assigning a string to that single character which you can't do. You probably meant one of the following:
1 2
secondStr[3] = 'a'; // makes the string "Thia is a second string"
secondStr = "a"; // makes the string "a"
No.
Counter example: char s[]="This string can be modified safely.";
You're thinking of string literals.
I misread.
I remember being told (or read somewhere) that the C++ std::string class was immutable as well.
Whoever told you that didn't know what the hell they were talking about.
secondStr[3] = "a"
std::string::operator[]() returns either a char &, or a const char &, depending on which side of the assignment operator the call appears.
You're basically trying to do this: char a="whoops";
I really have to apologize, you are absolutely right. The string class is mutable. I wonder where i heard it was immutable, since i remember the example clearly.
It was about string::replace. Since one had to use it like str = str.replace(arg0,arg1,...);