how to remove emtpy element

this split function works if passing a value fine, but if it is not passed a value sep, to auto split the string by whitespace. Its kind of doing it. It is adding an empty element into the vector. I am just not sure how to check for an empty element in c++ in order to avoid pushing that empty index to the back (line 21)? or to just re loop an remove, but that seems kind of redundant.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void split(const string s, vector<string> &v, char sep=NULL){
    string index;
    for (int i=0; i<s.length(); i++){
        if (sep != NULL){
            if (s[i] == sep){
                v.push_back(index);
                index = "";
            }
            else{
                index += s[i];
            }
        }
        else{
            if (s[i] == ' ' || s[i] == '\t' || s[i] == '\n'){
                v.push_back(index);
                //std::cout << "element is: " << index << std::endl;
                index = "";
            }
            else{
                index += s[i];
            }
        }
    }
    v.push_back(index);
    for (int i=0; i<s.length(); i++){
        //remove empty elements
    }
}

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

int main(){
    vector<string> v;
    split("some random string sitting here", v,' ');
    print(v);
    std::cout << '\n';
    vector<string> v2;
    split("some random string\nsit\nting \t\t\n   \n\there\n", v2);
    print(v2);
}


1
2
3
4
5
6
test.cpp: In function ‘void split(std::string, std::vector<std::basic_string<char> >&, char)’:
test.cpp:10:20: warning: NULL used in arithmetic [-Wpointer-arith]
test.cpp: In function ‘int main()’:
test.cpp:48:66: warning: converting to non-pointer type ‘char’ from NULL [-Wconversion-null]
[some][random][string][sitting][here]
[some][random][string][sit][ting][][][][][][][][][here][]
Last edited on
line 8:
string index = "";

line 11:
if (s[i] == sep && index.length())

line 20:
if ( (s[i] == ' ' || s[i] == '\t' || s[i] == '\n') && index.length() )

http://www.cplusplus.com/reference/cctype/isspace/
Last edited on
oh ok thanks:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

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

int main(){
    vector<string> v;
    split("some random string sitting here", v,' ');
    print(v);
    std::cout << '\n';
    vector<string> v2;
    split("some random string\nsit\nting \t\t\n   \n\there\n", v2);
    print(v2);

}



what about the warnings?
Last edited on
Topic archived. No new replies allowed.