久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 C++ 中的 std::string 中刪除空格

Remove spaces from std::string in C++(從 C++ 中的 std::string 中刪除空格)
本文介紹了從 C++ 中的 std::string 中刪除空格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 C++ 中從字符串中刪除空格的首選方法是什么?我可以遍歷所有字符并構建一個新字符串,但有沒有更好的方法?

What is the preferred way to remove spaces from a string in C++? I could loop through all the characters and build a new string, but is there a better way?

推薦答案

最好的做法是使用算法 remove_if 和 isspace:

The best thing to do is to use the algorithm remove_if and isspace:

remove_if(str.begin(), str.end(), isspace);

現在算法本身不能改變容器(只能修改值),所以它實際上將值打亂并返回一個指向現在結束位置的指針.所以我們必須調用string::erase來實際修改容器的長度:

Now the algorithm itself can't change the container(only modify the values), so it actually shuffles the values around and returns a pointer to where the end now should be. So we have to call string::erase to actually modify the length of the container:

str.erase(remove_if(str.begin(), str.end(), isspace), str.end());

我們還應該注意,remove_if 最多只會制作一份數據副本.這是一個示例實現:

We should also note that remove_if will make at most one copy of the data. Here is a sample implementation:

template<typename T, typename P>
T remove_if(T beg, T end, P pred)
{
    T dest = beg;
    for (T itr = beg;itr != end; ++itr)
        if (!pred(*itr))
            *(dest++) = *itr;
    return dest;
}

這篇關于從 C++ 中的 std::string 中刪除空格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 97视频在线观看免费 | 999久久久| 久久综合社区 | 黄色大片在线播放 | 三级a毛片| 四虎永久在线视频 | 成人午夜网站 | 在线免费看毛片 | 亚洲免费播放 | 日韩欧美自拍 | 成人免费看片视频 | 国产三级在线观看视频 | 久久黄色大片 | 99在线精品视频 | www色| 成人一区二区三区 | 免费观看全黄做爰大片视频美国 | 国产成人综合在线 | 亚洲精彩视频 | 国产深夜福利 | 国产资源视频 | 在线播放黄色 | 小镇姑娘国语版在线观看免费 | 在线观看欧美日韩视频 | 欧美精品黄色 | 亚洲欧美综合网 | www.色中色 | 二级黄色片 | 亚洲高清在线 | 一级毛片黄色 | 日本不卡一区 | 久久国产精品免费视频 | 成年人免费看视频 | 久草资源在线观看 | 操出白浆视频 | 免费av不卡| 欧美日韩一本 | 国产福利在线播放 | 久久精品一区二区国产 | 日韩偷拍自拍 | 97国产精品人人爽人人做 |