c++ - How to reverse an std::string? -


this question has answer here:

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

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -