string parser comments

Started by
1 comment, last by iNfuSeD 20 years, 7 months ago
kay this is my first attempt at making a string parser. it seems to work fine but i''m unsure if it is conventional or not. any comments?

#include <cctype>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void main() {
	string input;
	vector<string> words;
	int i,p;
	while (getline(cin,input)) 
	{
		i=p=0;
		if (input=="exit") break;
		while (i<=input.length())
		{
			if (isspace(input[i]) || i==input.length()) 
			{
				words.push_back(input.substr(p, i-p));
				p=i+1;
			}
			i++;
		}
		cout << input << endl;
		for(i=0; i<=(words.size()-1); i++) cout<<words[i] << endl;
		words.erase(words.begin(), words.end());
		
	}

}

"The human mind is limited only by the bounds which we impose upon ourselves." -iNfuSeD
Advertisement
Have a look at stream iterators. I can't remember exactly, but I think this is how you would make something nice and generic. Oh, and I'd put your parsing into a separate function.
#include <iostream>#include <vector>#include <string>#include <sstream>#include <iterator>#include <algorithm>// think that's all you need... :] template <class Container, typename CharT, typename Traits>void parse(Container& c, const std::basic_string<CharT, Traits>& str){   std::basic_istringstream<CharT, Traits> iss(str);   std::copy(std::istream_iterator<std::basic_string<CharT, Traits>, CharT, Traits>(iss),             std::istream_iterator<std::basic_string<CharT, Traits>, CharT, Traits>(),             std::back_inserter(c));} templaete <class Container, typename CharT, typename Traits>void parse(Container& c, const std::basic_istream<CharT, Traits>& is){   std::copy(std::istream_iterator<std::basic_string<CharT, Traits>, CharT, Traits>(is),             std::istream_iterator<std::basic_string<CharT, Traits>, CharT, Traits>(),             std::back_inserter(c));} int main() {   std::vector<std::string> vec;   std::string input;    while (std::getline(std::cin, input) && input != "exit")      parse(vec, input); /*   or   parse(vec, std::cin);   and force EOF*/     std::copy(vec.begin(), vec.end(),             std::ostream_iterator<std::string>(std::cout, "\n"));    std::cout << "Press any key to exit...";   std::cin.get();    return 0;}

Sorry, but I don't have a compiler on this machine, so I haven't tested this. I might have made a few silly mistakes, but just play around with it.

Edit: Yes, silly mistake. :]

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 21, 2003 4:40:38 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
would you mind explaining how that code works? i understand the syntax and i''ve followed all the variables through and see where they go.. but i have no clue as to what it is doing to them to parse them into a vector of strings.
"The human mind is limited only by the bounds which we impose upon ourselves." -iNfuSeD

This topic is closed to new replies.

Advertisement