Skip to content

Instantly share code, notes, and snippets.

@szolotykh
Created June 28, 2019 23:57
Show Gist options
  • Save szolotykh/1c1aafd41986b3399cbf225263160354 to your computer and use it in GitHub Desktop.
Save szolotykh/1c1aafd41986b3399cbf225263160354 to your computer and use it in GitHub Desktop.
CString Split C++
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include "cstring.h"
using namespace std;
void CString::split(std::vector<CString>& strings)
{
stringstream ss;
ss << m_string;
while(!ss.eof())
{
string out;
ss >> out;
strings.push_back(out);
}
}
std::ostream& operator<<(std::ostream& os, const CString& str)
{
os << str.m_string;
return os;
}
std::istream& operator>>(std::istream& os, CString& str)
{
os >> str.m_string;
return os;
}
class CString
{
public:
CString ()
:m_string("")
{
}
CString (std::string str)
:m_string(str)
{
}
CString(const char str[])
:m_string(str)
{
}
public:
void split(std::vector<CString>& strings);
friend std::ostream& operator<<(std::ostream& os, const CString& str);
friend std::istream& operator>>(std::istream& os, CString& str);
private:
std::string m_string;
};
std::ostream& operator<<(std::ostream& os, const CString& str);
std::istream& operator>>(std::istream& os, CString& str);
#include <iostream>
#include <vector>
#include "cstring.h"
using namespace std;
int main()
{
CString str = "What is your name";
vector<CString> strings;
str.split(strings);
for(auto s : strings){
cout << s << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment