解决方案感谢 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;
}
============================================
我今天试图练习和学习如何使用多维数组,但遇到了一个问题。我不知道如何将一个字符串拆分为多个字符串,每隔 N 行。我在网上搜索过,但似乎只有空格和标记的示例。反正有没有做我想做的事?
例子:
#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;
}