Ok that works, but it looks to me like I am deleting arr and then somehow setting a non existent object equal to arr2?
Since it works that clearly isn't the case, but strange.
Ok that works, but it looks to me like I am deleting arr and then somehow setting a non existent object equal to arr2?
Since it works that clearly isn't the case, but strange.
a pointer is a "pointer". It is not a object, and it does not have a value. It points to a address that contains the data. When you use "new" it returns the address to the allocated memory.
thus when you "delete" a pointer, the pointer is not changed, the data at the address is freed. That is why everyone says to NULL your pointers after freeing them.
Looks like I'll do some more reading, thanks
I actually made my own generic Split function, as well. It has the advantage of working on any container which supports the interface of std::string. Perhaps it can be enhanced with some traits, but that is for another time.
It also supports splitting a string, and not a token:
In case someone finds it useful, instead of strtok.Code:namespace Strings { // Splits a string taken by argument strToSplit with the string passed as strSplitWhat and returns a vector of the splitted strings template<typename T> std::vector<T> Split(const T& strToSplit, const char* strSplitWhat) { return Split<T>(strToSplit, (T)strSplitWhat); } // Splits a string taken by argument strToSplit with the string passed as strSplitWhat and returns a vector of the splitted strings template<typename T> std::vector<T> Split(const T& strToSplit, const T& strSplitWhat) { std::vector<T> vec; typename T::const_iterator it = strToSplit.begin(); typename T::const_iterator last_it = it; const typename T::const_iterator begin_it = strToSplit.begin(); const typename T::const_iterator end_it = strToSplit.end(); while (it != end_it) { std::string::size_type i = strToSplit.find(strSplitWhat, it - begin_it); if (i == std::string::npos) it = end_it; else { it = begin_it + i; vec.push_back( strToSplit.substr(last_it - begin_it, it - last_it) ); last_it = it + strSplitWhat.size(); ++it; } } vec.push_back( strToSplit.substr(last_it - begin_it) ); return vec; } }
I actually made my own generic Split function, as well. It has the advantage of working on any container which supports the interface of std::string. Perhaps it can be enhanced with some traits, but that is for another time.
It also supports splitting a string, and not a token:
In case someone finds it useful, instead of strtok.Code:namespace Strings { // Splits a string taken by argument strToSplit with the string passed as strSplitWhat and returns a vector of the splitted strings template<typename T> std::vector<T> Split(const T& strToSplit, const char* strSplitWhat) { return Split<T>(strToSplit, (T)strSplitWhat); } // Splits a string taken by argument strToSplit with the string passed as strSplitWhat and returns a vector of the splitted strings template<typename T> std::vector<T> Split(const T& strToSplit, const T& strSplitWhat) { std::vector<T> vec; typename T::const_iterator it = strToSplit.begin(); typename T::const_iterator last_it = it; const typename T::const_iterator begin_it = strToSplit.begin(); const typename T::const_iterator end_it = strToSplit.end(); while (it != end_it) { std::string::size_type i = strToSplit.find(strSplitWhat, it - begin_it); if (i == std::string::npos) it = end_it; else { it = begin_it + i; vec.push_back( strToSplit.substr(last_it - begin_it, it - last_it) ); last_it = it + strSplitWhat.size(); ++it; } } vec.push_back( strToSplit.substr(last_it - begin_it) ); return vec; } }