简体   繁体   中英

Ignore spaces in vector C++

I'm trying to split a string in individual words using vector in C++. So I would like to know how to ignore spaces in vector, if user put more than one space between words in string.

How would I do that?

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

using namespace std;

int main(){

    cout<<"Sentence: ";
    string sentence;
    getline(cin,sentence);

    vector<string> my;
    int start=0;
    unsigned int end=sentence.size();

unsigned int temp=0;

while(temp<end){
    int te=sentence.find(" ",start);
    temp=te;
    my.push_back(sentence.substr(start, temp-start));
    start=temp+1;
}

unsigned int i;
    for(i=0 ; i<my.size() ; i++){

        cout<<my[i]<<endl;

    }

return 0;
}

Four things:

  1. When reading input from a stream into astring using the overloaded >> operator, then it automatically separates on white-space. Ie it reads "words".

  2. There exists an input stream that uses a string as the input, std::istringstream .

  3. You can use iterators with streams, like eg std::istream_iterator .

  4. std::vector have a constructor taking a pair of iterators.

That means your code could simply be

std::string line;
std::getline(std::cin, line);

std::istringstream istr(line);

std::vector<std::string> words(std::istream_iterator<std::string>(istr),
                               std::istream_iterator<std::string>());

After this, the vector words will contain all the "words" from the input line.

You can easily print the "words" using std::ostream_iterator and std::copy :

std::copy(begin(words), end(words),
          std::ostream_iterator<std::string>(std::cout, "\n"));

The easiest way is to use a std::istringstream like follows:

std::string sentence;
std::getline(std::cin,sentence);
std::istringstream iss(sentence);
std::vector<std::string> my;
std::string word;
while(iss >> word) {
    my.push_back(word);
}

Any whitespaces will be ignored and skipped automatically.

You can create the vector directly using the std::istream_iterator which skips white spaces:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>

int main() {
    std::string str = "Hello World  Lorem Ipsum  The    Quick Brown Fox";
    std::istringstream iss(str);
    std::vector<std::string> vec {std::istream_iterator<std::string>(iss),
        std::istream_iterator<std::string>() };
    for (const auto& el : vec) {
        std::cout << el << '\n';
    }
}

Here is a function which divides given sentence into words.

#include <string>
#include <vector>
#include <sstream>
#include <utility>

std::vector<std::string> divideSentence(const std::string& sentence) {
    std::stringstream stream(sentence);
    std::vector<std::string> words;
    std::string word;
    while(stream >> word) {
        words.push_back(std::move(word));
    }
    return words;
}

Reducing double, triple etc. spaces in string is a problem you'll encounter again and again. I've always used the following very simple algorithm:

Pseudocode:

while "  " in string:
    string.replace("  ", " ")

After the while loop, you know your string only has single spaces since multiple consecutive spaces were compressed to singles.

Most languages allow you to search for a substring in a string and most languages have the ability to run string.replace() so it's a useful trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM