⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10-4(5,6).cpp

📁 Accelerated C++ 课后练习题 本人自己完成、可供参考
💻 CPP
字号:
#include<iostream>
#include<vector>
#include<string>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::cin;
using std::getline;
struct record_info
{
	string s;
	struct record_info *next,*front;
};
class String_list
{
public:
	typedef struct record_info* ptr;
	String_list();
	string name(ptr record) const
	{
		return record->s;
	}
	ptr begin() const
	{
		return first;
	}
	ptr end() const
	{
		return last->next;
	}
	ptr rbegin() const
	{
		return last;
	}
	void push_back(ptr);
	void push_front(ptr);
private:
	ptr first,last;
};
void String_list::push_back(ptr x)
{
	x->front=x->next=NULL;
	if(last==NULL)
		first=last=x;
	else
	{
		x->front=last;
		last->next=x;
		last=x;
	}
}
void String_list::push_front(ptr x)
{
	x->front=NULL;
	x->next=first;
	if(first==NULL)
		last=first=x;
	else
	{
		first->front=x;
		first=x;
	}
}
String_list::String_list():first(NULL),last(NULL)
{
}
String_list split(const string& s)
{
	String_list ret;
	typedef string::size_type string_size;
	string_size i=0;

	//ivarinant: we have processed characters [original value of i, i)
	while(i!=s.size())
	{
		//ignore leading blanks
		//invariant: characters in range [original i, current i) are all spaces
		while(i!=s.size()&&isspace(s[i]))
			++i;
		//find end of next word
		string_size j=i;
		//invariant: none of the characters in range [original j,current j)is a space
		while(j!=s.size()&&!isspace(s[j]))
			j++;
		//if we found some nonwhitespace characters
		if(i!=j)
		{
			//copy from s starting at i and taking j-i chars
			String_list::ptr p=new struct record_info;
			p->s=s.substr(i,j-i);
			ret.push_back(p);
			i=j;
		}
	}
	return ret;
}

int main()
{
	String_list	L;
	string s;
	getline(cin,s);
	L=split(s);
	for(String_list::ptr i=L.begin();i!=L.end();i=i->next)
		cout<<L.name(i)<<endl;
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -