M
Marcus Kwok
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the
"all characters" marker. I tried to use it this way, but my program
crashes:
#include <iostream>
#include <string>
int main()
{
std::string s = "hi/hello/now";
std::cout << s << '\n';
for (std::string::size_type i = 0; i != std::string::npos; ++i) {
if (s == '/') {
s = '\\';
}
}
std::cout << s << '\n';
}
If I change "std::string::npos" to "s.length()" then of course the
program works correctly. Why doesn't std::string::npos work in this
context?
"all characters" marker. I tried to use it this way, but my program
crashes:
#include <iostream>
#include <string>
int main()
{
std::string s = "hi/hello/now";
std::cout << s << '\n';
for (std::string::size_type i = 0; i != std::string::npos; ++i) {
if (s == '/') {
s = '\\';
}
}
std::cout << s << '\n';
}
If I change "std::string::npos" to "s.length()" then of course the
program works correctly. Why doesn't std::string::npos work in this
context?