-3

I want to copy the words in the chars array to the vector string.I wrote the following code and it gives me errors that string,vector,words wasn't declared in this scope,and I declared the function in the header file can you help??

here is the code:

vector<string> split(char sentence[])
{
    vector<string> ans(100);
    int count=0;
    for(unsigned int i=0;i<sentence.size();i++)
    {
    if(sentence[i]==' ')
        count=count+1;
    ans[count]=ans[count]+sentence[i];
    }
    return ans;
}
avakar
  • 32,009
  • 9
  • 68
  • 103

3 Answers3

3

A char[] is a primitive type, and does not have member functions, for example no such thing as .size()...

Are you sure you know what you are doing? This, and the missing of headers (which Luchian already commented on) give the impression you don't, really...

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • 2
    Note also that the primitive type `char[]` is used nowhere in the code. The actual type of the parameter is `char*`. – Konrad Rudolph Oct 29 '12 at 14:52
  • @KonradRudolph: Actually, the language lawyer in me would like to disagree on that one, as it is *declared* as `char[]`. In the end, it doesn't make a difference in the context of the question. You might be more correct, I might be more understandable, but in any case, `sentence.size()` is broken code. – DevSolar Oct 29 '12 at 14:55
  • 1
    The point is that the type of `sentence` is `char*`, not `char[]`. You're saying that `char[]` doesn't have `size` member, but that is technically irrelevant here. – avakar Oct 29 '12 at 15:00
  • Konrad and avakar are correct - the type of `sentence` *is* `char *`. I was only aware of the practical implications of passing an array as parameter (e.g. that you cannot take `sizeof` to determine its length anymore), not that it actually implied a type change. – DevSolar Oct 29 '12 at 15:15
0

You need to include the headers <vector> and <string> and qualify the use with std:: or use using directives. Full name qualification should be preferred:

std::string
std::vector

EDIT: I didn't notice the char*::size mistake, because I was focused on the error message you posted ("wasn't declared in this scope"). Some thought this deserved a downvote... whatever.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

You really ought to just use a built-in library or something, and split the string with a regular expression.

Instead of using a char[] use a string. Its easy to convert char[] to that, and it was probably a string to begin with, so you should cut that out earlier.

To form your vector<string>, all you have to do is this:

#include <regex.h>
#include <string.h>
#include <vector.h>

using namespace std;

vector<string> split(string s){
    regex r ("\\w+"); //regex matches whole words, (greedy, so no fragment words)
    regex_iterator<string::iterator> rit ( s.begin(), s.end(), r );
    regex_iterator<string::iterator> rend; //iterators to iterate thru words
    vector<string> result<regex_iterator>(rit, rend);
    return result;  //iterates through the matches to fill the vector
}

That would probably need one or two bugs worked out (I'm just a little rusty), and could also be compacted down considerably using inline statements.

Remember: The magic of c++ comes in two forms: iterators and inline statements.

AJMansfield
  • 4,039
  • 3
  • 29
  • 50