Jewfit
Jewfit

Reputation: 11

How do you remove multiple characters in the strings that are held in a vector, in c++?

I want to delete characters of a string that are in a vector, starting from an index inputted by the user up until the end of that string. For example if in index 0, my vector has the string "hello" and index 1 has the string, "goodbye", I want to erase the characters "llo" in the first string and "dbye" in the second string. So the result will be "he" in index 0 and "goo" in index 1. In my code that I am posting, I did not add the part of getting input from the user for the index. But just pretend, it is index 4 and beyond. How would I do this? Thank you.

I tried putting a '\0' character at the index that I want to start the deletion at, but that does not work.

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

using namespace std;

int main()
{
    int maxSize;
    cin >> maxSize;
    string usrInput;
    vector<string> myArray;

    for(int i = 0; i < maxSize; i++)
    {
        cin >> usrInput;
        myArray.push_back(usrInput);
    }

    myArray[0][4] = '\0';

    cout << myArray[0];

    return 0;
}

Upvotes: 1

Views: 2683

Answers (3)

Klaus
Klaus

Reputation: 25613

the std::string class provides a method substr which do the job.

Example:

int main()
{
    int maxSize;
    cin >> maxSize;
    string usrInput;
    vector<string> myArray;

    for(int i = 0; i < maxSize; i++)
    {
        cin >> usrInput;
        myArray.push_back(usrInput);
    }

    for ( auto& s: myArray )
    {
        s = s.substr(0,4);
        std::cout << s << std::endl;
    }



    return 0;
}

http://www.cplusplus.com/reference/string/string/substr/

or

https://en.cppreference.com/w/cpp/string/basic_string/substr

Or you may want use resize which let the resulting string to be at a fixed size, if needed, filled up by a char which you can add as parameter.

https://en.cppreference.com/w/cpp/string/basic_string/resize

What you did is simply replacing a character inside the string. So if you have a string "abcdef" you will get "abcd\0e" which is not what you expect. You can see that yourself by printing out each of the chars like this:

   myArray[0][4]='\0';

   for ( auto&c: myArray[0] )
   {
       std::cout << (int) c << std::endl;
   }

If you print it as a c-string, the output looks like the string is shorted, but it is really not! This one looks well but is wrong:

myArray[0][4]='\0';
std::cout << myArray[0].c_str() << std::endl;

Why?: Quite simple: std::cout uses for printing std::string a different method than for printing c-style strings.

Upvotes: 3

Ted Lyngmo
Ted Lyngmo

Reputation: 117298

You can combine the std::string method substr with the standard algorithm std::for_each to apply your cutting function to all strings in the vector.

#include <algorithm> // std::for_each

std::cout << "cut at length: ";

if(size_t cutpoint; std::cin >> cutpoint) {
    std::for_each(myArray.begin(), myArray.end(), [&cutpoint](std::string& str) {
        str = str.substr(0, cutpoint);
    });
}

Upvotes: 5

PauZen
PauZen

Reputation: 102

If you know where you want to cut, this is why substr function exist. Its a method from string, look at the documentation http://www.cplusplus.com/reference/string/string/substr/.

Hope it helps.

Upvotes: 2

Related Questions