如何正确使用这个迭代器?

时间:2022-02-27 00:16:08

I'm trying to use a container to store the iterator of string s, but I got segment fault when I run it. As you will see in the following, the error seems to come with char temp = **itbegin;, it might means I can't assign any value via the previous iterator.

我正在尝试使用容器来存储字符串s的迭代器,但是当我运行它时我遇到了段错误。正如您将在下面看到的,错误似乎来自char temp = ** itbegin;,这可能意味着我无法通过前一个迭代器分配任何值。

Why is it? Did I misuse the iterator?How to use the iterator properly?

为什么?我是否误用了迭代器?如何正确使用迭代器?

#include <iostream>
#include <vector>
using namespace std;
string reverseVowels(string s);

int main()
{
    string s ="hello";
    cout << reverseVowels(s);
}
string reverseVowels(string s) {
    string::iterator iter = s.begin();
    string::iterator iterend = s.end();
    vector<string::iterator> iteratorbox;
    for(;iter != iterend ; ++iter){
        if((*iter) == 'a' &&
           (*iter) == 'e' &&
           (*iter) == 'i' &&
           (*iter) == 'o' &&
           (*iter) == 'u'){
               iteratorbox.push_back(iter);
           }

    }
    auto itbegin = iteratorbox.begin();
    auto itend =   iteratorbox.end() ;
    --itend;
    //for(;itbegin < itend ; ++itbegin, --itend)
    {
        char temp = **itbegin;
    //    *(*itbegin) = *(*itend); 
    //    *(*itend) = temp;
    }
    return s;
}

2 个解决方案

#1


6  

Your problem comes from the condition upon which you insert iterators in your iteratorbox vector.

您的问题来自于在iteratorbox向量中插入迭代器的条件。

You used the && operator, implying that every letter of the string has to be equal to all vowels at the same time. This means no iterator will be inserted, and you are then trying to dereference the begin() iterator of the vector, which happens to be its past-the-end iterator. This causes undefined behavior, which in your case manifests itself as a crash.

你使用了&&运算符,暗示字符串的每个字母必须同时等于所有元音。这意味着不会插入迭代器,然后您尝试取消引用向量的begin()迭代器,它恰好是它的过去的迭代器。这会导致未定义的行为,在您的情况下表现为崩溃。

You probably meant to use

你可能想要使用

((*iter) == 'a' ||
(*iter) == 'e' ||
(*iter) == 'i' ||
(*iter) == 'o' ||
(*iter) == 'u')

as a condition.

作为一个条件。

#2


0  

The answer by @rems4e is perfectly fine, but I find such code easier to read and less error-prone if you put the vowels into an array

@ rems4e的答案非常好,但是如果你将元音放入一个数组中,我发现这样的代码更容易阅读并且更不容易出错

char const vowels[] = { 'a', 'e', 'i', 'o', 'u' };

so that you can encapsulate the matching logic inside your reverseVowels into the Standard algorithm any_of

这样您就可以将reverseVowels中的匹配逻辑封装到标准算法any_of中

if (std::is_any_of(std::begin(vowels), std::end(vowels), [](auto const& v) {
   return *iter == v;
}) {
    iteratorbox.push_back(iter);
}

This avoids much of the repetitive testing (which can get out-of-sync quickly should you ever use a different alphabet (German e.g.))

这避免了大量的重复测试(如果您使用不同的字母表,可能会很快失去同步(例如德语))

#1


6  

Your problem comes from the condition upon which you insert iterators in your iteratorbox vector.

您的问题来自于在iteratorbox向量中插入迭代器的条件。

You used the && operator, implying that every letter of the string has to be equal to all vowels at the same time. This means no iterator will be inserted, and you are then trying to dereference the begin() iterator of the vector, which happens to be its past-the-end iterator. This causes undefined behavior, which in your case manifests itself as a crash.

你使用了&&运算符,暗示字符串的每个字母必须同时等于所有元音。这意味着不会插入迭代器,然后您尝试取消引用向量的begin()迭代器,它恰好是它的过去的迭代器。这会导致未定义的行为,在您的情况下表现为崩溃。

You probably meant to use

你可能想要使用

((*iter) == 'a' ||
(*iter) == 'e' ||
(*iter) == 'i' ||
(*iter) == 'o' ||
(*iter) == 'u')

as a condition.

作为一个条件。

#2


0  

The answer by @rems4e is perfectly fine, but I find such code easier to read and less error-prone if you put the vowels into an array

@ rems4e的答案非常好,但是如果你将元音放入一个数组中,我发现这样的代码更容易阅读并且更不容易出错

char const vowels[] = { 'a', 'e', 'i', 'o', 'u' };

so that you can encapsulate the matching logic inside your reverseVowels into the Standard algorithm any_of

这样您就可以将reverseVowels中的匹配逻辑封装到标准算法any_of中

if (std::is_any_of(std::begin(vowels), std::end(vowels), [](auto const& v) {
   return *iter == v;
}) {
    iteratorbox.push_back(iter);
}

This avoids much of the repetitive testing (which can get out-of-sync quickly should you ever use a different alphabet (German e.g.))

这避免了大量的重复测试(如果您使用不同的字母表,可能会很快失去同步(例如德语))