C++输入流,输入多行,用list存入邻接矩阵

该博客主要介绍了在C++中利用输入流输入多行数据,并将其用list存入邻接矩阵的相关内容,聚焦于C++编程和数据存储操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++输入流,输入多行,用list存入邻接矩阵

#include "pch.h"
#include <iostream>//cout
#include <list>//list
#include <sstream>//istringstream
#include <string>//string
using namespace std;
int main() {
	list<list<int>> AdjacentList;
	string s;
	while (getline(cin, s)) {
		istringstream iss(s);
		list<int> temp;//list<int> *temp = new list<int>;if you use this instead,you should change variable "temp" below to "*temp".
		for (int number; iss >> number; temp.push_back(number)) {
		}
		AdjacentList.push_back(temp);
	}
	//using ctrl+z and enter in new line to end inputing.
	for (auto it0 = AdjacentList.begin(); it0 != AdjacentList.end(); ++it0) {
		for (auto it1 = it0->begin(); it1 != it0->end(); ++it1) {
			cout << *it1 << " ";
		}
		cout << endl;
	}
	//Both OK
	//for (list<list<int>>::iterator it0 = AdjacentList.begin(); it0 != AdjacentList.end(); ++it0) {
	//	for (list<int>::iterator it1 = it0->begin(); it1 != it0->end(); ++it1) {
	//		cout << *it1 << " ";
	//	}
	//	cout << endl;
	//}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值