c++ - How to reverse an std::string? -
this question has answer here:
- how reverse string in place in c or c++? 27 answers
im trying figure out how reverse string temp
when have string read in binary numbers
istream& operator >>(istream& dat1d, binary& b1) { string temp; dat1d >> temp; }
i'm not sure mean string contains binary numbers. reversing string (or stl-compatible container), can use std::reverse()
. std::reverse()
operates in place, may want make copy of string first:
#include <algorithm> #include <iostream> #include <string> int main() { std::string foo("foo"); std::string copy(foo); std::cout << foo << '\n' << copy << '\n'; std::reverse(copy.begin(), copy.end()); std::cout << foo << '\n' << copy << '\n'; }
Comments
Post a Comment