帮忙看下下面的程序为什么有错误

极光默尘 2013-05-06 02:30:23
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
typedef struct transaction
{
vector<int>read;
}tran;
int main()
{
vector<tran>table(0);
int count=0;
char s[1000];
ifstream infile;
char *p;
infile.open("C:\\Users\\liwei\\Desktop\\MATI\\T2.txt");
if(infile==NULL)
{
cout<<"File is not exist"<<endl;
exit(0);
}
else if(infile.is_open())
{
cout<<"File open succeed."<<endl;
while(infile.getline(s,1000,'\n'))
{
p = strtok(s,",");
while(p)
{
table[atoi(p)].read.push_back(count);
p = strtok(NULL,",");
}
count++;
}
}
infile.close();
}
我写这个的想法主要是为了读取T2.txt中
1,2,3,4
2,3,4
我想一次读取一行,就是用vector一次读取1,2,3,4,并且用strok把逗号去掉,然后想存放一个二维数组里面,为什么程序运行不出来呢
...全文
125 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
buyong 2013-05-08
  • 打赏
  • 举报
回复
引用 5 楼 jiguang0455 的回复:
那假如我现在的资料是t.txt: 1,2,3,4,5 2,3,7 8,9 4,7 我现在想用垂直的结构表示,就是比如1是出现在第一行,2出现在第一行、第二行,3出现在第一行和第二行,这样的表示形式,该怎么做呢
use a database
极光默尘 2013-05-07
  • 打赏
  • 举报
回复
楼上的方法是在知道有几行几列的情况下才能实现吧?
ForestDB 2013-05-06
  • 打赏
  • 举报
回复
map<int, vector<int> > m; m[1].push_back(1); m[2].push_back(1), m[2].push_back(2); m[3].push_back(1), m[3].push_back(2);
极光默尘 2013-05-06
  • 打赏
  • 举报
回复
那假如我现在的资料是t.txt: 1,2,3,4,5 2,3,7 8,9 4,7 我现在想用垂直的结构表示,就是比如1是出现在第一行,2出现在第一行、第二行,3出现在第一行和第二行,这样的表示形式,该怎么做呢
hugett 2013-05-06
  • 打赏
  • 举报
回复
插入table的时候可能会造成越界。。

			while(p)	
			{
				int k = atoi(p);
				if(k + 1 > table.size()) table.resize(k + 1);//先判断是否越界,否则增大table的size。。
				table[k].read.push_back(count);
				p = strtok(NULL,",");
			}
极光默尘 2013-05-06
  • 打赏
  • 举报
回复
看错了,看菜一个地方弄错了,现在就是想问下//table[atoi(p)].read.push_back(count);//这样的结构很吓人,呵呵 t.read.push_back(atoi(p));这个为什么要这么做呢,可以解释下吗
极光默尘 2013-05-06
  • 打赏
  • 举报
回复
for(j=0;j<table[i].read.size();j++){ cout<<table[i].read[j]<<" "; 为什么我的却显示cout<<没有符合的运算元呢,编译不了
码农写博客 2013-05-06
  • 打赏
  • 举报
回复
可以了

#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
typedef struct transaction
{
	vector<int>read;
}tran;
int main()
{
	vector<tran> table;
	int count=0;
	char s[1000];
	ifstream infile;
	tran t;
	char *p;
	infile.open("C:\\Users\\liwei\\Desktop\\MATI\\T2.txt");
	if(infile==NULL)
	{
		cout<<"File is not exist"<<endl;
		exit(0);
	}
	
	else if(infile.is_open())
	{
		cout<<"File open succeed."<<endl;
		while(infile.getline(s,1000,'\n'))
		{
			p = strtok(s,","); 
			while(p) 
			{
			//	table[atoi(p)].read.push_back(count);//这样的结构很吓人,呵呵
				t.read.push_back(atoi(p));
				
				
				p = strtok(NULL,",");
			}
		
			table.push_back(t);
			t.read.clear();
		
		}
		
		
	}
	int i,j;
         i=j=0;
	for(i=0;i<table.size();i++){

		for(j=0;j<table[i].read.size();j++){
			cout<<table[i].read[j]<<" ";
		}
		cout<<"第"<<i<<"行"<<endl;
	
	}

	infile.close();
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧