how can I do this?? can someone help me
char * abc = "as,df,ert:we,rt,yu:ee,rr,tt";
Class x
{
char * a,*b,*c;
}
vector<x> vtr;
I want to extract data from abc like,
while(there is a ":")
{
x.a= as;
x.b=df;
x.c=ert;
vtr.push_back(x);
}
how can I do this?? can someone help me
char * abc = "as,df,ert:we,rt,yu:ee,rr,tt";
Class x
{
char * a,*b,*c;
}
vector<x> vtr;
I want to extract data from abc like,
while(there is a ":")
{
x.a= as;
x.b=df;
x.c=ert;
vtr.push_back(x);
}
Here is the c++ way of doing that
#include<iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class X
{
public:
string a;
string b;
string c;
};
int main()
{
string abc = "as,df,ert:we,rt,yu:ee,rr,tt";
vector<X> vtr;
X xx;
size_t pos;
while( (pos = abc.find(':')) != string::npos)
{
string s = abc.substr(0,pos);
abc = abc.substr(pos+1);
stringstream str(s);
getline(str,xx.a,',');
getline(str,xx.b,',');
getline(str,xx.c);
vtr.push_back(xx);
}
return 0;
}
If you are looking for a C solution then post your question in the C board.
> Here is the c++ way of doing that
Two things:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
class X {
std::vector<std::vector<std::string> > _parts;
public:
X(const std::string& src);
const int size() const;
const std::vector<std::string>& operator[](int i) const;
};
X::X(const std::string& src)
{
std::string::size_type begin = 0;
std::string::size_type end;
do {
end = src.find(':', begin);
std::istringstream iss(src.substr(begin, end - begin));
std::string input;
_parts.resize(_parts.size() + 1);
while (std::getline(iss, input, ','))
_parts.back().push_back(input);
begin = end;
} while (begin++ != std::string::npos);
}
const int X::size() const
{
return _parts.size();
}
const std::vector<std::string>& X::operator[](int i) const
{
return _parts.at(i);
}
int main()
{
X x("as,df,ert:we,rt,yu:ee,rr,tt");
for (int i = 0; i < x.size(); ++i) {
for (int j = 0; j < x[i].size(); ++j)
std::cout << x[i][j] << ' ';
std::cout << '\n';
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.