How to reverse an std::string?
177,738
I'm not sure what you mean by a string that contains binary numbers. But for reversing a string (or any STL-compatible container), you can use std::reverse()
. std::reverse()
operates in place, so you may want to make a copy of the 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';
}
Author by
Admin
Updated on February 17, 2020Comments
-
Admin over 4 years
Im trying to figure out how to reverse the string
temp
when I have the string read in binary numbersistream& operator >>(istream& dat1d, binary& b1) { string temp; dat1d >> temp; }