Getting all suffixes of a string in c++ -
in c 1 can declare char array, create pointer specific location in array, , dereference suffix in specified position:
char str[6] = "sleep"; char* pointer = &(str[1]); printf("%s\n", pointer); the above code print
leep which suffix of "sleep" in position 1. there way similar in c++ without using substr method produces new string , i'm trying avoid that.
you can you've done.
char str[6] = "sleep"; char* pointer = &(str[1]); printf("%s\n", pointer); if using std::string instead of raw char buffer (like should be), under c++11 std::string guaranteed have contiguous storage (21.4.1/5) appended nul terminator (21.4.7.1/1):
std::string str = "sleep"; const char* pointer = &str[1]; these guarantees new c++11 -- c++03 makes no such guarantees. implementations i'm aware of in fact use contigious storage appended nul terminator. because c_str() required return const pointer c-style string. if want solution guaranteed compliant in c++03, std::vector makes same contiguity guarantee, in c++03, of course there must apply nul terminator on own:
std::string load = "sleep"; vector <char> str; copy (load.begin(), load.end(), back_inserter (str)); load.push_back ('\0'); // appended nul terminator const char* pointer = &str [1]; but we're talking making copies, obviously.
Comments
Post a Comment