c++ - Implementing a split() function -
accelerated c++, exercise 14.5 involves reimplementing split function (which turns text input vector of strings). 1 must use store input in std::string - class (str) & use split function return vec<str>, vec being std::vector - container. str class manages custom pointer (ptr) underlying vec<char> data.
the str class provides constructor str(i,j), in str.h below, constructs ptr underlying vec problem arises when try create substrings calling str(i,j) i've detailed in code the issues arise. here whittled-down version of str class (can post more code if needed):
str.h
#include "ptr.h" #include "vec.h" class str { friend std::istream& operator>>(std::istream&, str&); public: // define iterators typedef char* iterator; typedef char* const_iterator; iterator begin() { return data->begin(); } const_iterator begin() const { return data->begin(); } iterator end() { return data->end(); } const_iterator end() const { return data->end(); } //** define constructor `ptr`s substrings ** template<class in> str(in i, in j): data(new vec<char>) { std::copy(i, j, std::back_inserter(*data)); } private: // store ptr vec ptr< vec<char> > data; }; split.h
vec<str> split(const str& str) { typedef str::const_iterator iter; vec<str> ret; iter = str.begin(); while (i != str.end()) { // ignore leading blanks = find_if(i, str.end(), not_space); // find end of next word iter j = find_if(i, str.end(), space); // copy characters in `[i,' `j)' if (i != str.end()) ret.push_back(**substring**); // need create substrings here // call str(i,j) gives error, detailed below = j; } return ret; } my first thought use constructor create (pointers to) required substrings. calling str(i,j) here gives error message
type 'const str' not provide call operator - it appears if 1 cannot call
str(i,j)here. why not? - could solution write
strmember function similarsubstr?
Comments
Post a Comment