久久久久久久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 入門)
主站蜘蛛池模板: 伊人天堂网 | av高清在线 | 亚洲视频在线看 | 一区二区三区国产 | 中文字幕1区 | 黄色网址入口 | 特级丰满少妇一级aaaa爱毛片 | 成人免费毛片嘿嘿连载视频 | 亚洲影音| 欧美一区二区三区在线 | 亚洲精品视频免费在线观看 | 国产精品久久久一区二区 | 日韩精品在线免费观看 | 欧美一二三 | 免费不卡视频 | 国产精品欧美在线 | 男女啪啪免费 | 国产xxx| 四虎四虎| 日本黄色中文字幕 | 在线一区二区三区 | 国产一区二区三区在线观看视频 | 久久久久综合 | 福利视频网址导航 | 国产精品天美传媒入口 | 99热国产在线 | 国产精品一区二区三区在线 | 狠狠操夜夜操 | 日韩高清一区二区 | 天天干网| 老司机午夜免费精品视频 | 99这里有精品| 婷婷久久综合 | 黄色国产视频 | 久久视频在线免费观看 | 少妇高潮久久久久久潘金莲 | 欧美区一区二 | 日本高清在线观看 | 国产福利精品视频 | 久久久国产视频 | 欧美日韩a |