Tokenizing user input

This is a discussion on Tokenizing user input within the C++ Programming forums, part of the General Programming Boards category; Ok that works, but it looks to me like I am deleting arr and then somehow setting a non existent ...

  1. #16
    Registered User
    Join Date
    Sep 2008
    Posts
    33
    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.

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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.

  3. #18
    Registered User
    Join Date
    Sep 2008
    Posts
    33
    Looks like I'll do some more reading, thanks

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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:
    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;
    	}
    }
    In case someone finds it useful, instead of strtok.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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:
    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;
    	}
    }
    In case someone finds it useful, instead of strtok.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 10:23 AM
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21