...
return the character stored at the specified position if pos < < size()
. If pos == size()
, the const
version returns the terminating null character type value. Otherwise, the behavior is undefined.
...
Code Block |
---|
|
string bs(&quot;01234567&quot;"01234567");
size_t i = f();
bs[i] = '\0';
|
...
Wiki Markup |
---|
This compliant solution uses the {{basic_string at()}} method, which behaves in a similar fashion to the index {{operator\[\]}} but throws an {{out_of_range}} exception if {{pos &gt;>= size()}}. |
Code Block |
---|
|
string bs(&quot;01234567&quot;"01234567");
try {
size_t i = f();
bs.at(i) = '\0';
}
catch (...) {
cerr &lt;&lt; &quot;<< "Index out of range&quot; &lt;&lt;" << endl;
}
|
In any case, the behavior of the index operators is unchecked (no exceptions are thrown).
...
Code Block |
---|
|
string bs(&quot;01234567&quot;"01234567");
for (int i=0; i &lt;< 100; i++) {
bs[i] = '\0';
}
|
...
Code Block |
---|
|
size_t const max_fill = 100;
std::string bs(&quot;01234567&quot;"01234567");
fill(bs.begin(), bs.begin()+std::min(max_fill, bs.length()), '\0' );
|
...