76

Im trying to figure out how to reverse the string temp when I have the string read in binary numbers

istream& operator >>(istream& dat1d, binary& b1)    
{              
    string temp; 

    dat1d >> temp;    
}
1
  • 2
    What do you mean by "read in binary numbers"? That the string will have something like "1100110"? That you're reading a binary file off of disk? Commented Feb 10, 2011 at 0:15

2 Answers 2

117

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';
}
Sign up to request clarification or add additional context in comments.

4 Comments

It should be a void function since it does not return a value
std::reverse is a void function: en.cppreference.com/w/cpp/algorithm/reverse .
@MaxLybbert Yes, however, main() as it is implemented is not void and should return a value. This is what @Wilhelm was trying to point out.
I can add the "return 0", but it so happens that C++ has a special case for main: "if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;" ( en.cppreference.com/w/cpp/language/main_function , 4th item on the special properties list).
67

Try

string reversed(temp.rbegin(), temp.rend());

EDIT: Elaborating as requested.

string::rbegin() and string::rend(), which stand for "reverse begin" and "reverse end" respectively, return reverse iterators into the string. These are objects supporting the standard iterator interface (operator* to dereference to an element, i.e. a character of the string, and operator++ to advance to the "next" element), such that rbegin() points to the last character of the string, rend() points to the first one, and advancing the iterator moves it to the previous character (this is what makes it a reverse iterator).

Finally, the constructor we are passing these iterators into is a string constructor of the form:

template <typename Iterator>
string(Iterator first, Iterator last);

which accepts a pair of iterators of any type denoting a range of characters, and initializes the string to that range of characters.

4 Comments

On your second line, did you mean to type "string reverse(.....)" instead of "string reversed(.....)" ? In other words, should the word "reversed" be "reverse" ?
@Job_September_2020 it's just a variable name, can be anything
@HighCommander4 how did u get those rbegin, rend? i dont have those, only begin(), end()
@datdinhquoc They are new in C++11, see en.cppreference.com/w/cpp/string/basic_string/rbegin

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.