SOLUTION Thanks to 1111…
vector<std::string> split_at_line(string str, int lines) {
vector<std::string> nine_ln_strs;
string temp;
stringstream ss;
int i = 0;
while(i != str.length()) {
ss << str.at(i);
if(i == lines) {
lines += lines;
getline(ss,temp);
nine_ln_strs.push_back(temp);
ss.clear();
temp.clear();
}
if(i+lines > str.length()) {
getline(ss,temp);
nine_ln_strs.push_back(temp);
ss.clear();
temp.clear();
break;
}
i++;
}
return nine_ln_strs;
}
===========================================
I was trying to practice and learn how to work with multidimensional arrays today and I came to a problem. I have no idea how I can split a string into multiple strings, every Nth line. I have searched the web but it seems there is only examples for white-spaces, and tokens. Is there anyway to do what I want to do?
Example:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
const int five = 5;
int test[][five] = {
{ 0, 0, 1, 0, 0 },
{ 0, 1, 1, 0, 0 },
{ 0, 2, 1, 0, 0 }
};
int main() {
stringstream result;
int a = sizeof test / sizeof ( test[0] );
cout << a << endl;
int b = 5;
for ( int i = 0; i < a; i++ ) {
for ( int j = 0, inc = 0 ; j < b; j++, inc++ ) {
if(inc == 2) {
result << hex << setfill ('0') << setw(4) << (int)test[i][j];
} else {
result << hex << setfill ('0') << setw(2) << (int)test[i][j];
}
}
}
string s = result.str();
cout << s << endl;
// split the string into segments of every 000000000000 and store them into a new string each time, or another array
int z; // hold
cin >> z;
return 0;
}
There might be a nicer way of doing it but this should work
PS: C array aren’t very nice try and use containers where possible. Also making a constant called
fivedefined to5isn’t very helpful. The idea of using a variable here is so you can change it later, if you changed the value of five to6then that would just be weird.test_szwould be a better name.