c ++ stl_输入元素并将其添加到列表| C ++ STL

c ++ stl

To implement this example, we need to include "list" header that contains the functions for list operations.

要实现此示例,我们需要包括“列表”标头,其中包含用于列表操作的功能。

To add the elements to the list, we use push_back() function, which is a library function of "list" header.

要将元素添加到列表中,我们使用push_back()函数 ,它是“列表”标头的库函数。

Example:

例:

Here, we are declaring a list of string, reading string and adding them to the list.

在这里,我们声明一个字符串列表,读取字符串并将其添加到列表中。

#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() 
{
	//declaring a list
	list<string> lstr;
	//declaring iterator to the list
	list<string>::iterator l_iter;
	
	//declaring string object
	string str;

	//input strings
	while(true)
	{
		cout<<"Enter string (\"EXIT\" to quit): ";
		getline(cin, str);
		if(str=="EXIT")
			break;

		//adding string to the list 
		lstr.push_back(str);
	}

	//printing list elements
	cout<<"List elements are"<<endl;
	for (l_iter = lstr.begin(); l_iter != lstr.end(); l_iter++)
		cout<< *l_iter<<endl;

	return 0;
}

Output

输出量

Enter string ("EXIT" to quit): New Delhi
Enter string ("EXIT" to quit): Mumbai
Enter string ("EXIT" to quit): Chennai
Enter string ("EXIT" to quit): EXIT
List elements are
New Delhi
Mumbai
Chennai


翻译自: https://www.includehelp.com/stl/input-and-add-elements-to-a-list.aspx

c ++ stl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值