I am aware of why segmentation faults occur, but I am not able to find out the error with the following code I made to split a string on the basis of whitespaces.
#include<iostream>
#include<string>
#include<vector>
#include<typeinfo>
using namespace std;
vector<string> split(const string& s)
{
//cout << "HERE";
vector<string> tab;
for(unsigned int a = 0; a < s.size(); a++)
{
string temp = to_string(s[a]);
while(to_string(s[a]) != " ")
{
a++;
temp = temp + s[a];
}
tab.push_back(temp);
}
return tab;
}
int main()
{
int n;
cin >> n;
while(n--)
{
string s;
cin >> s;
vector<string> temp = split(s);
for(unsigned int i = 0; i < temp.size(); i++)
{
cout << temp[i] << endl;
}
}
return 0;
}
Also, if I comment out the while loop in the split function, I get numbers when I print out the resulting strings. Is it because of to_string? If I use typeid(variable).name() on the resulting strings that I get when I am printing them in the main function,this is what I get : NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Aucun commentaire:
Enregistrer un commentaire