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

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

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

問題描述

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

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);

現(xiàn)在算法本身不能改變?nèi)萜?只能修改值),所以它實際上將值打亂并返回一個指向現(xiàn)在結(jié)束位置的指針.所以我們必須調(diào)用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());

我們還應(yīng)該注意,remove_if 最多只會制作一份數(shù)據(jù)副本.這是一個示例實現(xiàn):

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

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

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

相關(guān)文檔推薦

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?(如何計算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡(luò)攝像機的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時導(dǎo)出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 黄色大片在线播放 | 国产乱性 | 日韩欧美在线视频一区 | 中文av字幕 | 日韩一区二区免费视频 | 中文字幕高清免费日韩视频在线 | 干狠狠| 精品久久久久一区二区国产 | 亚洲一区二区三区福利 | 8x国产精品视频一区二区 | 在线资源视频 | 在线免费观看a级片 | 欧美一区免费 | 一区二区三区四区在线视频 | 精品在线一区 | 精品国产青草久久久久福利 | 色视频成人在线观看免 | 精品久久一区二区 | 青青久久久 | 一区二区国产精品 | 久久久久中文字幕 | 日韩成人在线观看 | 男女羞羞视频在线 | 亚洲一区二区三区 | 99视频免费看| 亚洲永久免费 | 三级黄色片在线观看 | 日韩久久久久久 | 成人免费黄视频 | av国产精品 | 特一级毛片 | 精品一区二区三 | 欧美日本免费 | 久久久久国产 | 欧美成人一区二免费视频软件 | 网站黄色在线 | 国产精品色 | 亚洲国产精品va在线看黑人 | 羞羞的视频在线看 | 国产精品精品久久久 | 欧美福利|