Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

removing items (strings) from vector<string>

34 views
Skip to first unread message

RM

unread,
Aug 3, 2020, 11:32:59 AM8/3/20
to
I need to remove from identifiers[i_properties] (which is
vector<string>) all items (strings) that can be found in
identifiers[i_variables] (which is vector<string>, too).

for (string v : identifiers[i_variables]) {
auto it = find(identifiers[i_properties].begin(),
identifiers[i_properties].end(), v);
if (it != identifiers[i_properties].end()) {
identifiers[i_properties].erase(it);
}
}

It doesn't work. Strings are found but not erased.

Real Troll

unread,
Aug 3, 2020, 12:29:56 PM8/3/20
to
I believe you need to assign an  index of the found item and then erase
it.  Try or change this code:

>     for (string v : identifiers[i_variables]) {
>         auto it = find(identifiers[i_properties].begin(),
> identifiers[i_properties].end(), v);
>         if (it != identifiers[i_properties].end()) {
>             int index = std::distance(identifiers.begin(), it);
>             identifiers.erase (identifiers.begin()+index);
>         }
>     }

I have assumed that identifiers is your vector and this may or may not
be correct as your snippet is a bit cryptic for me!!.





Öö Tiib

unread,
Aug 3, 2020, 12:51:02 PM8/3/20
to
Since it is random whiny garbage not program. Program looks like that ...:

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

size_t i_variables = 0;
size_t i_properties = 1;
std::vector<std::string> identifiers[2] = {
{"removing", "items", "from", "what"},
{"from", "noob", "what"}
};

int main() {

// your posted garbage begins
for (std::string v : identifiers[i_variables]) {
auto it = find(identifiers[i_properties].begin()
, identifiers[i_properties].end(), v);
if (it != identifiers[i_properties].end()) {
identifiers[i_properties].erase(it);
}
}
// your posted garbage ends

for (auto const& v : identifiers[i_properties]) {
std::cout << v << '\n';
}
}

... and it seems to work.
Demo: <http://coliru.stacked-crooked.com/a/3bc5c9364eb72e31>






Sam

unread,
Aug 4, 2020, 7:01:11 AM8/4/20
to
Says who? What is your proof that "strings are found but not erased"?

RM

unread,
Aug 4, 2020, 8:00:05 AM8/4/20
to
W dniu 03.08.2020 o 17:32, RM pisze:
My mistake. Strings in identifiers[i_variables] were prefixed with $, in
identifiers[i_properties] were not.
Problem closed.
0 new messages