std::string str = "Hello, World!"; std::string substr = str.substr(7, 5); // "World"
std::string str = "The quick brown fox"; std::string::iterator start = str.begin() + 4; std::string::iterator end = str.end() - 4; std::string substr = str.substr(start - str.begin(), end - start); // "quick brown"In this example, we create a string "The quick brown fox". We then create two iterators "start" and "end", which point to the 4th position from the beginning and end of the string, respectively. We then call the substr function, specifying the starting position as the distance between the "start" iterator and the beginning of the string, and the length as the distance between the "start" and "end" iterators. The resulting substring is then assigned to the "substr" string. Package Library: The C++ string substr function is part of the standard C++ library, which is included in most C++ compilers and development environments.