Aside form adding those arguments to compile command, is there another way to do this without getting warnings? Also is the warnings the reason to why whitespace other than a space are being ignored?
#include <iostream>
#include <string>
#include <vector>
usingnamespace 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 (s[i] == ' ' || s[i] == '\n' || s[i] == '\t'){
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(){
string s;
vector<string> v;
while (true){
std::cout << "\n: ";
getline(cin, s);
split(s, v);
print(v);
v.clear();
}
/*
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);
*/
}
output:
1 2 3 4 5 6 7 8 9
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:49:19: warning: converting to non-pointer type ‘char’ from NULL [-Wconversion-null]
: test\t \t \ttest
[test\t][\t][\ttest]
:
Aside form adding those arguments to compile command, is there another way to do this without getting warnings?
To get rid of these warnings you just have to fix the (minor) defects in your code.
sep is of type char so you should be testing it against the null char '\0' not NULL, which for old-style pointers (C++11 code should use nullptr instead of NULL.)
I aslo changed the type of i from int to size_t, as that's in step with what string::size returns (an unsigned rather than signed value.)
1 2 3 4 5 6 7 8 9 10 11 12 13
void split(const string s, vector<string> &v, char sep='\0'){
string index; // string sets itself to "" automatically
for (size_t i=0; i<s.length(); i++){
if (sep != '\0'){
if (s[i] == sep){
v.push_back(index);
index.clear(); // rather than = "";
}
else{
index += s[i];
}
}
// etc
Also is the warnings the reason to why whitespace other than a space are being ignored?
Nope -- looks like it should work ok (for spaces and tabs)
(both NULL and '\0' are zero, so aside from the cast, the maths will be ok.)
you should be testing it against the null char '\0' not NULL
i didnt know there was one specifically for char, thanks.
off topic but regarding the same code:
After that modification though the output is not what i expect it. It appears that it is not splitting the string up by tabs and newlines, but only by a single space?
the output:
1 2 3 4 5 6 7
: test test
[test][test]
: test\ttest
[test\ttest]
: test\ntest
[test\ntest]
:
i expected the output to be:
1 2 3 4 5 6 7
: test test
[test][test]
: test\ttest
[test][test]
: test\ntest
[test][test]
:
When the compiler issues a warning diagnostic, it is trying to help the programmer by pointing out a possible error in the code. Do not turn them off by default; while writing code, we need all the help that we can get. Compile with warnings enabled at the highest level available.
If after we have looked at a warning, analyzed it, and satisfied ourselves that things are ok despite the warning, we can opt to suppress that specific warning for that specific part of code by appropriate #pragma directives. (Ideally along with a comment explaining why it has been disabled).
For instance, with the GNU toolchain:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <string>
#include <iostream>
int main()
{
std::string str = "abcdef" ;
// turn off the warning: comparison between signed and unsigned integer expressions
// we know that it is safe to ignore it in this particular case
#pragma GCC diagnostic push // save the current state
#pragma GCC diagnostic ignored "-Wsign-compare"
for( int i = 0 ; i < str.size() ; ++i ) std::cout << str[i] ;
#pragma GCC diagnostic pop // restore the previous state
// ... ;
}