首页 > 其他 > 详细

vector读入指定行数但不指定列数的数字

时间:2019-04-04 14:01:31      阅读:159      评论:0      收藏:0      [点我收藏+]
 1 #include <iostream>
 2 #include <vector>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <vector>
 6 #include <stdlib.h>
 7 #include <stdio.h>
 8 #include <string>
 9 #include <string.h>
10 
11 using namespace std;
12 vector <int> gridTopo[6];
13 
14 void readTxt()
15 {
16     char readLine[1000];
17     const char *delim = " ";
18     char *p;
19     for (int i = 0; i < 6; i++)
20     {
21         cin.getline(readLine, 1000);
22         p = strtok(readLine, delim);
23         while (p)
24         {
25             gridTopo[i].push_back(atoi(p));
26             p = strtok(NULL, delim);
27         }
28     }
29 }
30 
31 int main()
32 {
33     readTxt();
34     for (int i = 0; i < 6; i++)
35     {
36         for (std::vector<int>::iterator m = gridTopo[i].begin(); m != gridTopo[i].end(); m++)
37             cout << *m << " ";
38         cout << endl;
39     }
40     system("pause");
41     return 0;
42 }

如代码所示,假设输入6行数字,每行输入的具体数字不确定。

用getline读取每行的字符串,用strtok命令找出所有" "并把数字压入vector。

注意读取时特定可用gridTopo[0][0]代表第一行第一个数字,遍历可用vector的begin和end来完成不确定列数的输入。

vector读入指定行数但不指定列数的数字

原文:https://www.cnblogs.com/ljy1227476113/p/10654332.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!