-5

I have an iterator of type:

std::map<int, std::string>::const_iterator

and as you can see the map is storing std::strings.

I am trying to extract a value using:

x.second.c_str()

and pass it to a function to call the regex.h function regexec(), which accepts a char*. What is the best way to do this? I am getting basic_string compilation errors from this approach.

Update:

std::map<int, std::string> map;
.
.
.
std::map<int, std::string>::const_iterator f = map.find("something");
if (f != map.end()) {
    my_func(f->second.c_str());   
.
.
.
.

void my_func(char* c){
    std::cout << c << std::endl;   //This causes a segmentation fault
}
Biffen
  • 6,249
  • 6
  • 28
  • 36
user997112
  • 29,025
  • 43
  • 182
  • 361
  • 1
    What is x? If it is an iterator you should do x->second.c_str() – Alan Jun 03 '14 at 14:35
  • Please provide an [SSCCE](http://sscce.org/), so that others can understand your exact problem. – Csq Jun 03 '14 at 14:37
  • Duplicate of http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char?rq=1 – Bruno Ferreira Jun 03 '14 at 14:39
  • **1)** `regexec` seems to accept a `const char*`, which is a huge difference. **2)** If you go to the mechanic and say "something's wrong with my car" he wont be able to help. Same applies here: Show the car code and tell us exactly *what* is wrong with it, i.e. provide the error messages. – Arne Mertz Jun 03 '14 at 14:41
  • x->second.c_str() maybe? – remi Jun 03 '14 at 14:41
  • Have added all the code. – user997112 Jun 03 '14 at 14:41
  • what's the compiler saying? – remi Jun 03 '14 at 14:42
  • That is **not** all the code, or you have a very strange compiler. – Arne Mertz Jun 03 '14 at 14:43
  • First you have written about `regexec` and compilation errors related to `basic_string`, but now your code shows another function, does not mention `regexec` and has a comment about segfaults. What is your actual problem? – Arne Mertz Jun 03 '14 at 14:46
  • Are you sure that map.find("something") is correct. The key in your map is an int. – Alan Jun 03 '14 at 14:47

2 Answers2

0

You should do

int error=regexec(&regex,cppstring.c_str(),nmatch,pmatch,eflags);

The second argument of regexec() and the return value of std::string::c_str() are both (const char *), so that's probably not the cause of your issue.

You should post the compiler error message and the part of the code that you suspect to be causing the problem.

humodz
  • 600
  • 3
  • 8
0

Here's a very wild guess, though I could be mislead by the contradictory and incomplete information given:

  • I assume you have a function that takes a char* as parameter, e.g. like the my_func provided in the question.
  • I further assume you try to dereference a const_iterator of some kind, ultimately getting a reference to a const string, and call c_str() on it.
  • The next assumption is that you want to pass the result of that call to your function.

That would look somewhat like this:

void my_func(char*);

int main() {
  std::map<int, std::string> theMap;
  // ...
  std::map<int, std::string>::const_iterator cit = theMap.find(3);
  assert(cit != theMap.end());


  my_func(cit->second.c_str()); //problematic line
}

In this case, you should get a compilation error, because c_str() returns a const char*. while your function expects a char*, and the two are not compatible.

However, my assumptions have several flaws:

  • The error should be about the conversion from const char* to char* and should not have to do anything with basic_string
  • It has nothing to do with regexec (but neither has your example code)
  • It cannot lead to segfaults, since it does not compile (but that contradiction is already present in your question)

If you shed some more light on the question, I will be able to give more specific hints on what is going wrong.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90