erase vowels from list<string>

I have to write a function that seeks for vowels inside list of strings.
After vowel is found it needs to be erased from list. After all vowels are erased list<string> is sorted from largest to smallest. I have tried something, but I am not very good with lists and iterators. This is what I've tried, please help me to fix this. I have put these lines in main() first to test if this would even compile.
Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<string>
#include<list>
using namespace std;
int main(){
string x;
list<string> vowel;
while(cin>>x)
vowel.push_back(x);
list<string>::iterator k;
for(auto k=vowel.begin(); k!=vowel.end(); k++)
if(*k == 'A' || *k =='a' || ...) // for every letter that is a vowel
vowel.erase(*k);
vowel.sort();
return 0;
}
Last edited on
Lambdas for Lambdas God!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<list>
#include<algorithm>


int main()
{
    std::list<std::string> vowel {"Hello", "World"};
    std::for_each(vowel.begin(), vowel.end(), [](std::string& s)
        {
            s.erase(std::remove_if(s.begin(), s.end(), [](char c)
                        {
                            const std::string v = "euioa";
                            return v.find(c) != std::string::npos;
                        }),
                    s.end());
        });
    vowel.sort(std::greater<std::string>());
    for(const auto& s: vowel)
        std::cout << s << '\n';
}
Wrld
Hll


Edit: serious version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<algorithm>
#include<cctype>
#include<iostream>
#include<list>
#include<string>



int main()
{
    std::list<std::string> vowel {"Hello", "World"};
    for(auto& s: vowel) {
        s.erase(std::remove_if(s.begin(), s.end(), [](char c)
                    {
                        static const std::string v = "euioa";
                        return v.find(std::tolower(c)) != std::string::npos;
                    }),
                s.end());
    }
    vowel.sort(std::greater<std::string>());
    for(const auto& s: vowel)
        std::cout << s << '\n';
}

Edit 2: Wait. Is "from largest to smallest" relates to string length or lexicographic order?
Last edited on
Using std::list like this is misuse. Use std::vector instead - std::list is only good for very few specialized use cases.
@MiiNiPaa thanks for your answer.
@L B: I know there are vectors, but I got this assigment, and I will be tested next week with something similar to this. So I am forced to do this with list<string>
Lambdas are one option, but I would like to know if there is workaround like dereferencing iterator and compare elements.
My mistake, only first element in string is removed if it is a vowel. I got a code that compiles fine but it seems to erase entire string instead of first letter only. It seems that method list.erase(i++) erases link to box with string. Here's my code. Thanks in advance.
http://bpaste.net/show/189476/
You need nested loops. One to iterate over list, other to iterato over individual characters. And I highly suggest you to use remove_if with custom predicate (make it freestanding function if you have aversion for lambdas). Or else you might run into problems with consequent vowels.

And shorten your line 14: use techniques from this thread (either my first or JLBorges one): http://www.cplusplus.com/forum/beginner/126227/
Topic archived. No new replies allowed.