forbids comparison between pointer and integer

the if condition in split() function: What is exactly going on? If I remove the pointer i get forbidden comparison between pointer and integer?

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> split(string s, vector<string> v){
	string index;
	for (int i=0; i<s.length(); i++){
		
		if (s[i] == *" "){
			v.push_back(index);
			index = "";
		}
		else{
			index += s[i];
		}
	}
	v.push_back(index);
	return v;
}

void print(vector<string> v){
	for (int i=0; i<v.size(); i++){
		cout << "[" << v[i] << "]";
	}
}

int main(){
	string s = "some random string sitting here";
	vector<string> v;
	v = split(s, v);
	print(v);
}
ok that was a simple fix.
if (s[i] == ' '){

Question: wouldnt looping a string return a string index? whereas its char
Last edited on
*" " What is this supposed to be?

A string is made up of chars, so each "step" you get a char in the string.

Also, what is the point of passing the vector v to split()? It seems like it just used as a temporary to hold the result you are going to return and may as well just be local to the function. If you passed a vector with data already inside, it would end up appending to it, which makes me wonder if that's really what you wanted.
I was thinking looping a string, each step would be a string also. And when i put " ", it gave the error "forbids comparison between pointer and integer", so i put the pointer there, which apparently worked. I dont know, was kind of playing around with it.

well my intention was to mimic python's string.split() method.
actually yeah, there is no point in passing v to split() and also returning it. I was debating about using a pointer to a vector or just returning a local vector, and i kind of meshed them together as i focused on the char problem.

yeah after that it would be:
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void split(const string s, vector<string> &v, char sep='\n'){
    string index;
    for (int i=0; i<s.length(); i++){
        if (s[i] == sep){
            v.push_back(index);
            index = "";
        }
        else{
            index += s[i];
        }
    }
    v.push_back(index);
}

void print(vector<string> v){
    for (int i=0; i<v.size(); i++){
        cout << "[" << v[i] << "]";
    }
}

int main(){
    string s = "some random string sitting here";
    vector<string> v;
    split(s, v,' ');
    print(v);
}
Last edited on
Topic archived. No new replies allowed.