0

I have two vectors. One char vector contains elements which each element stores a character of a paragraph (including dot . The other is a string vector whose each element should store a word created from the first vector.

Here is my code:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
  string source = "Vectors are sequence containers representing arrays that can change in size!";
  vector<char> buf(source.begin(),source.end());
  vector<string> word;
  size_t n = 0;
  size_t l = 0;
    for (size_t k = 0; k < buf.size(); k++) {
        if (buf[k] == ' ' || buf[k] == '\0') { 
            for (size_t i = n; i < k; i++) {
                word[l] = word[l] + buf[i];
            }
            n = k + 1;
            l++;
        }
    }
    for (size_t m = 0; m < word.size(); m++) {
        cout << word[m] << endl;
    }
  return 0;
}

And then the system said that:

Expression: vector subscript out of range

"this project" has triggered a breakpoint

Of course, I have tried so many way to concatenate buf elements into a single word element( using .push_back(), to_string(),...) but it always caught errors. I dont try with normal array or const char* data type since my exercise requires me to use string and vector only.

Eureka
  • 35
  • 1
  • 6
  • See [here](https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string) and [here](https://stackoverflow.com/questions/19137617/c-function-split-string-into-words) for examples of tokenizing a sentence into individual words. – Cory Kramer May 17 '21 at 17:30
  • You never `push_back` or `resize` the vector `word` so doing `word[l] = ...` is writing out of bounds and therefore undefined behavior – Cory Kramer May 17 '21 at 17:30
  • The vector `buf` is really totally useless. You can iterate the exact same way directly over `source`. – Some programmer dude May 17 '21 at 17:37

2 Answers2

0

If the problem is to create a vector of words from the string source there are simpler ways.

For example if you remember that the input extraction operator >> reads "words" (space-delimited strings) then you can use it to your favor with an input stream that can read from strings, like std::istringstream.

And if you learn that there's a std::vector constructor overload taking two iterators, and that there is a class for input stream iterators you can combine this into a simple three-statement program:

std::string source = "Vectors are sequence containers representing arrays that can change in size!";

std::istringstream source_stream(source);

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

Now the vector words will contain the words from the source string, and can be printed one by one:

for (auto& w : words)
{
    std::cout << w << '\n';
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Here's one way:

#include <stdio.h>

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

int main() {
  std::string const source =
      "Vectors are sequence containers representing arrays that can change in size!";

  std::vector<std::string> words;
  for (auto ibeg = source.begin(), iend = ibeg;;) {
    iend = std::find(ibeg, source.end(), ' ');
    words.emplace_back(ibeg, iend);
    if (iend == source.end()) break;
    ibeg = iend + 1;
  }

  for (auto const& w : words) puts(w.c_str());
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93