如何扭转一个std :: string的? [英] How to reverse an std::string?

查看:142
本文介绍了如何扭转一个std :: string的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去弄清楚如何扭转串温度时,我有字符串中的二进制数读
请大家帮忙

 的IStream&安培;运营商的GT;>(istream的&安培; dat1d,二元放大器; B1) {    串温度;    dat1d>>温度;
}


解决方案

我不知道你所说的包含二进制数字的字符串是什么意思。但对于扭转一个字符串(或任何STL兼容的容器),你可以使用的std ::反向()的std ::反向()工作到位,所以你可能想使字符串拷贝第一:

 的#include<&算法GT;
#包括LT&;&iostream的GT;
#包括LT&;串GT;诠释的main()
{
    的std :: foo的字符串(富);
    性病::字符串复制(富);
    性病::法院LT&;<富<<的'\\ n'<<复制和LT;<的'\\ n';    性病::扭转(copy.begin(),copy.end());
    性病::法院LT&;<富<<的'\\ n'<<复制和LT;<的'\\ n';
}

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

istream& operator >>(istream& dat1d, binary& b1)

 {

    string temp; 

    dat1d >> temp;    
}

解决方案

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';
}

这篇关于如何扭转一个std :: string的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆