简体   繁体   English

从向量中删除空元素

[英]Deleting empty elements from vector

I am trying to delete empty entries from std::vector .我正在尝试从std::vector删除空条目。 Here is a sample code, but something is wrong here.这是一个示例代码,但这里出了点问题。

#include <iostream>
#include <string>
#include<vector>
#include <cctype>

int main()
{
    std::vector<std::string> s1 = {"a"," ", "", "b","c","   ","d"};
    for (auto it = s1.begin(); it != s1.end() && isspace(*it); )
{
        it = s1.erase(it);
}

    std::cout<<"vector size = "<<s1.size();
    for (auto &i:s1) 
        std::cout<<i<<"\n";      

}

I am running a for loop to find out empty elements and deleting from there.我正在运行一个for循环来找出空元素并从那里删除。 There should be STL method too, but not sure how it will work.也应该有 STL 方法,但不确定它是如何工作的。

It seems you mean the following看来你的意思是以下

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

int main() 
{
    std::vector<std::string> v = { "a", " ", "", "b", "c", "   ", "d" };

    auto is_empty = []( const std::string &s )
    {
        return s.find_first_not_of( " \t" ) == std::string::npos;
    };

    v.erase( std::remove_if( std::begin( v ), std::end( v ), is_empty ), std::end( v ) );

    for ( const auto &s : v )
    {
        std::cout << "\"" << s << "\" ";
    }
    std::cout << std::endl;

    return 0;
}

The program output is程序输出是

"a" "b" "c" "d" 

As for your code then it is inefficient because you are trying to remove each found element separately and this loop for example至于你的代码,那么它是低效的,因为你试图分别删除每个找到的元素,例如这个循环

for (auto it = s1.begin(); it != s1.end() && isspace(*it); )
{
    it = s1.erase(it);
}

can iterate never because the first element is not satisfies the condition isspace(*it) that moreover is invalid.永远不能迭代,因为第一个元素不满足条件isspace(*it)而且是无效的。 That is you are supplying an object of the type std::string to a function that expects an object of the type char (more precisely of the type int ).也就是说,您将std::string类型的对象提供给需要char类型(更准确地说是int类型)的对象的函数。

If to use the C function isspace then the program can look the following way.如果要使用 C 函数isspace则程序可以如下所示。

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cctype>

int main() 
{
    std::vector<std::string> v = { "a", " ", "", "b", "c", "   ", "d" };

    auto is_empty = []( const std::string &s )
    {
        return std::all_of( std::begin( s ), std::end( s ), 
                            []( char c ) 
                            { 
                                return std::isspace( ( unsigned char )c );
                            } );
    };

    v.erase( std::remove_if( std::begin( v ), std::end( v ), is_empty ), std::end( v ) );

    for ( const auto &s : v )
    {
        std::cout << "\"" << s << "\" ";
    }
    std::cout << std::endl;

    return 0;
}

The program output is the same as shown above.程序输出与上图相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM