The string is messed up after erase

Hi, I was trying the erase method in the std::string.
The string is "David Hardman Junior", and I try to erase everything after the first space. Right after the erase, the string is not readable anymore.
Can anyone help me out here?

std::string str = "David Hardman Junior";
size_t pos = 0;
pos = str.find(" "); //pos is 5 in this case
while( pos != std::string::npos ){
std::string token = str.substr(0, pos); //the string is not readable after this line
str.erase(0, pos);
pos = str.find(" ");
}
You've misunderstodd the parameters for those functions. Re-reference them. Here is your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    std::string str("David Hardman Junior");
    size_t pos = 0;
    
    if((pos = str.find(' '))!=std::string::npos)
      str.erase(pos, str.size()-pos);
     
    std::cout << str;
    
    std::cin.get();
    return 0;
} 
Hi, mcleano

What I'm trying to do is to tokenize the phrase.
For example, if the phrase is "David Hardman Junior", I want to get "David", "Hardman", and "Junior".

str = "David Hardman Junior";
After I found the first Space, I want to delete "David ", so the string is left with "Hardman Junior".

I tried:
<code>
string str = "David Ardman Junior";
str.erase(0, 5);
</code>
After erase, my string is some weird characters.
Why?
Are you trying to create it or yourself? Or are you not aware of the strtok() token?

Here is an example program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <vector>

std::string createSubStr(const std::string &str, size_t pos);

int main()
{
    std::string str("David Hardman Junior");
    std::vector<std::string> strVector;
    std::string subStr;
    size_t pos = 0;
    int i = 0;
    
    while(!str.empty()) {
        if((pos = str.find(' '))==std::string::npos) {
           strVector.push_back(str);
           break;
        }
        else {
           subStr = createSubStr(str, pos);
           str.erase(0, pos+1);
           strVector.push_back(subStr);
           i++;
           subStr.clear();
        }
    }
    
    std::cin.get();
    return 0;
}

std::string createSubStr(const std::string &str, size_t pos)
{
    std::string subStr;
    
    subStr = str.substr(0, pos);
    
    return subStr;
}  
Last edited on
Thanks for your detailed solution mcleano.

However, I'm more interested to know how to use erase:
1
2
string str = "David Ardman Junior";
str.erase(0, 5);

what's going wrong with this erase?
The parameters for erase are: erase(starting_position, no._characters_to_be_removed)

In your example above you are starting at the first character, 0, and deleting the next 5 characters so it looks like this:

0 = D
1 = a
2 = v
3 = i
4 = d

These are the five characters removed leaving you with " Ardman Junior"; notice the space that is left before 'A'. This is why in my example 'pos' is equal to how many characters in the space is, but you have to add one to it to actually delete that character.

Does this clarify things a bit better?
Topic archived. No new replies allowed.