0

Is there a way to find and replace subset of a char*/string in a set?

Example:

std::set<char*> myset; myset.insert("catt"); myset.insert("world"); myset.insert("hello"); it = myset.subsetfind("tt"); myset.replace(it, "t"); 
1
  • 4
    For associative containers you should not use a pointer as the key, as then it will be pointer that is the key and not the data it points to. If you want the key to be a string, then you should use std::string. Commented Feb 3, 2017 at 16:40

4 Answers 4

1

There are at least three reasons why this won't work.

  1. std::set provides only the means to search the set for a value that compares equally to the value being searched for, and not to a value that matches some arbitrary portion of the value.

  2. The shown program is undefined behavior. A string literal, such as "hello" is a const char *, and not a char *. No self-respecting C++ compiler will allow you to insert a const char * into a container of char *s. And you can't modify const values, by definition, anyway.

  3. Values in std::set cannot be modified. To effect the modification of an existing value in a set, it must be erase()d, then the new value insert()ed.

std::set is simply not the right container for the goals you're trying to accomplish.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately ICC, GCC and Clang all compile with only GCC and Clang issuing a warning.
1

No, you can't (or at least shouldn't) modify the key while it's in the set. Doing so could change the relative order of the elements, in which case the modification would render the set invalid.

You need to start with a set of things you can modify. Then you need to search for the item, remove it from the set, modify it, then re-insert the result back into the set.

std::set<std::string> myset {"catt", "world", "hello"}; auto pos = std::find_if(myset.begin(), myset.end(), [](auto const &s) { return s.find("tt");}; if (pos != myset.end()) { auto temp = *pos; myset.remove(pos); auto p= temp.find("tt"); temp.replace(p, 2, "t"); myset.insert(temp); } 

Comments

1

You cannot modify elements within a set.

You can find strings that contain the substring using std::find_if. Once you find matching elements, you can remove each from the set and add a modified copy of the string, with the substring replaced with something else.

PS. Remember that you cannot modify string literals. You will need to allocate some memory for the strings.

PPS. Implicit conversion of string literal to char* has been deprecated since C++ was standardized, and since C++11 such conversion is ill-formed.

PPPS. The default comparator will not be correct when you use pointers as the element type. I recommend you to use std::string instead. (A strcmp based comparator approach would also be possible, although much more prone to memory bugs).

Comments

0

You could use std::find_if with a predicate function/functor/lambda that searches for the substring you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.