String 删除字符串的子字符串

String 删除字符串的子字符串,string,c++11,substring,erase,String,C++11,Substring,Erase,我有问题要解决。我必须创建接收两个参数的函数,一个是字符串类型,另一个是int。我必须输入句子,然后输入一个数字。我的程序必须删除与该数字相等的单词。我写了很多代码,但我有问题,例如,如果我输入数字1,或者如果我的句子中单词之间有很多空格,它不会删除它们。我不能使用迭代器。 这是我的密码 #include <iostream> #include <string> #include <algorithm> int Numberofwords(std::stri

我有问题要解决。我必须创建接收两个参数的函数,一个是字符串类型,另一个是int。我必须输入句子,然后输入一个数字。我的程序必须删除与该数字相等的单词。我写了很多代码,但我有问题,例如,如果我输入数字1,或者如果我的句子中单词之间有很多空格,它不会删除它们。我不能使用迭代器。 这是我的密码

#include <iostream>
#include <string>
#include <algorithm>

int Numberofwords(std::string s)
{
    int trigg (0),suff(0);

    int x(0),y(s.length()-1);

    while(s[x] == ' ')
        x++;

    for(int x(y);x>=0;x--)
    {
        if(s[x]==' ') suff++;
        if(s[x] != ' ') break;
    }

    for(;x<s.length()-suff;x++)
    {
        if(s[x] == ' ') trigg++;
        if((s[x] == s[x+1]) && s[x] == ' ') trigg--;
        if((s[x] == ' ' && s[x+1]==' \n')) trigg--;
    }
    trigg++;
    return trigg;
}

std::string Funkcija(std::string recenica, int n)
{
    int br(Numberofwords(recenica)),x(0),y(recenica.length()-1),suff(0),counter(0),trig(0);

    std::string s;

    if(n<1 || n>br) throw "Inputed number must be bigger than 0 and smaller   then number of words in sentence.";

    while(recenica[x] == ' ')
        x++;

    for(int i(y);i>=0;i--)
    {
        if(recenica[i] == ' ') suff++;
        if(recenica[i] != ' ') break;
    }

    int a(x);
    for(;a<recenica.length()-suff;a++)
    {

        if(recenica[a] == ' ') trig++;

        if(trig == (n-1))
        {
            int e(a);
            while(recenica[e+1] != ' ')
            {
                counter++;
                e++;
            }

            recenica.erase(a+1,counter);
        }

    }
    return recenica;
}

您可以通过迭代字符串来实现这一点

字符串迭代可以用不同的方法完成:您想要第三种方法,但是如果可以,我推荐第一种或第二种方法

// Way #1: Iterators
std::string remove_word_by_number(std::string source, unsigned int word_number)
{
    if ( word_number == 0 )
        throw runtime_error("word_number cannot be 0");

    std::ostringstream out;

    bool word_removed = false;
    bool inside_word = false;
    unsigned int words_parsed = 0;
    for ( std::string::iterator it = source.begin(); it != source.end(); ++it )
    {
        if ( isalnum(*it) )
        {
            if ( !inside_word )
            {
                inside_word = true;
                words_parsed++;
            }
            if ( words_parsed != word_number )
                out << *it;
            else
                word_removed = true;
        }
        else
        {
            inside_word = false;
            out << *it;

            if ( word_removed )
            {
                out << std::string(it, source.end());
                break;
            }
        }
    }

    return out.str();
}

// Way #2: C-style iterators (pointers, not sure if you can use this kind of iterators)
std::string remove_word_by_number(std::string source, unsigned int word_number)
{
    if ( word_number == 0 )
        throw runtime_error("word_number cannot be 0");

    std::ostringstream out;

    bool word_removed = false;
    bool inside_word = false;
    unsigned int words_parsed = 0;
    for ( const char* it = source.data(); it != source.data()+source.size(); it++ )
    {
        if ( isalnum(*it) )
        {
            if ( !inside_word )
            {
                inside_word = true;
                words_parsed++;
            }
            if ( words_parsed != word_number )
                out << *it;
            else
                word_removed = true;
        }
        else
        {
            inside_word = false;
            out << *it;

            if ( word_removed )
            {
                out << source.substr(it-source.data());
                break;
            }
        }
    }

    if ( !word_removed )
        throw runtime_error("word_number out of range");

    return out.str();
}

// Way #3: Using string's operator[]
std::string remove_word_by_number(std::string source, unsigned int word_number)
{
    if ( word_number == 0 )
        throw runtime_error("word_number cannot be 0");

    std::ostringstream out;

    bool word_removed = false;
    bool inside_word = false;
    unsigned int words_parsed = 0;
    for ( std::size_t it = 0; it < source.size(); it++ )
    {
        if ( isalnum(source[it]) )
        {
            if ( !inside_word )
            {
                inside_word = true;
                words_parsed++;
            }
            if ( words_parsed != word_number )
                out << source[it];
            else
                word_removed = true;
        }
        else
        {
            inside_word = false;
            out << source[it];

            if ( word_removed )
            {
                out << source.substr(it);
                break;
            }
        }
    }

    return out.str();
}
希望能有帮助