You are viewing a single comment's thread. Return to all comments →
Can someone tell how can i make this code more shorter :- `#include
using namespace std;
vector parseInts(const string& str) { vector numbers; int current_number = 0; bool is_number = false; bool is_negative = false;
for (char ch : str) { if (isdigit(ch)) { current_number = current_number * 10 + (ch - '0'); is_number = true; } else if (ch == '-') { if (is_number) { numbers.push_back(is_negative ? -current_number : current_number); current_number = 0; is_number = false; } is_negative = true; } else if (ch == ',' || ch == ' ') { if (is_number) { numbers.push_back(is_negative ? -current_number : current_number); current_number = 0; is_number = false; is_negative = false; } } }
if (is_number) { numbers.push_back(is_negative ? -current_number : current_number); } return numbers;
}
int main() { string str; getline(cin, str); vector integers = parseInts(str); for (int i : integers) { cout << i << "\n"; }`
return 0;
`
Seems like cookies are disabled on this browser, please enable them to open this website
StringStream
You are viewing a single comment's thread. Return to all comments →
Can someone tell how can i make this code more shorter :- `#include
include
include
include
include
using namespace std;
vector parseInts(const string& str) { vector numbers; int current_number = 0; bool is_number = false; bool is_negative = false;
for (char ch : str) { if (isdigit(ch)) { current_number = current_number * 10 + (ch - '0'); is_number = true; } else if (ch == '-') { if (is_number) { numbers.push_back(is_negative ? -current_number : current_number); current_number = 0; is_number = false; } is_negative = true;
} else if (ch == ',' || ch == ' ') { if (is_number) { numbers.push_back(is_negative ? -current_number : current_number); current_number = 0; is_number = false; is_negative = false;
} } }
}
int main() { string str; getline(cin, str);
vector integers = parseInts(str); for (int i : integers) { cout << i << "\n"; }`
}
`