none
定义一个结构体容器,怎么输出容器里的元素? RRS feed

  • 问题

  • struct zifu
     {
      string a1;
      string a2;
      string a3;
      int b1;
     }zifu1;
     vector<zifu>a;
     zifu1.a1='a';
     zifu1.a2='b';
     zifu1.a3='c';
     zifu1.b1=1;
     a.push_back(zifu1);
     zifu1.a1='d';
     zifu1.a2='e';
     zifu1.a3='f';
     zifu1.b1=2;
     a.push_back(zifu1);

    以上代码怎么输出容器里的元素或将容器里的元素写入txt文件?(我通过重载输出运算符也不行,可能我重载输出运算符有误)

    • 已编辑 长流 2013年7月25日 2:25
    2013年7月24日 11:21

答案

  • #include "stdafx.h"
    
    #include <iostream>
    #include <fstream> 
    #include <vector>
    #include <string>
    using namespace std;
    
    struct zifu
    {
    	string a1;
    	string a2;
    	string a3;
    	int b1;
    }zifu1;
    
    ostream& operator<<(ostream& out, const zifu& zf)
    {
    	out<<"a1: "<<zf.a1<<"a2: "<<zf.a2<<"; a3: "<<zf.a3<<"; b1: "<<zf.b1;
    	return out;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {	
    	vector<zifu>a;
    	zifu1.a1='a';
    	zifu1.a2='b';
    	zifu1.a3='c';
    	zifu1.b1=1;
    	a.push_back(zifu1);
    	zifu1.a1='d';
    	zifu1.a2='e';
    	zifu1.a3='f';
    	zifu1.b1=2;
    	a.push_back(zifu1);
    	for(vector<zifu>::const_iterator iter = a.begin(); iter != a.end(); ++iter)
    	{
    		cout<<*iter<<endl;
    	}
    
    	// Write File
    	ofstream file;
    	file.open("E:\\11.txt");
    	char buf[32] = {0};
    	string strText;
    	for(vector<zifu>::const_iterator iter = a.begin(); iter != a.end(); ++iter)
    	{
    		memset(buf, 0, sizeof(buf));
    		sprintf(buf, "%d", iter->b1);
    		strText += iter->a1 + ", " + iter->a2 + ", " + iter->a3 + "," + buf + "\r\n";
    	}
    	file.write(strText.c_str(), strText.length());
    	file.close();
    	return 0;
    }
    


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2013年7月25日 7:49
    版主

全部回复

  • #include "stdafx.h"
    
    #include <iostream>
    #include <fstream> 
    #include <vector>
    #include <string>
    using namespace std;
    
    struct zifu
    {
    	string a1;
    	string a2;
    	string a3;
    	int b1;
    }zifu1;
    
    ostream& operator<<(ostream& out, const zifu& zf)
    {
    	out<<"a1: "<<zf.a1<<"a2: "<<zf.a2<<"; a3: "<<zf.a3<<"; b1: "<<zf.b1;
    	return out;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {	
    	vector<zifu>a;
    	zifu1.a1='a';
    	zifu1.a2='b';
    	zifu1.a3='c';
    	zifu1.b1=1;
    	a.push_back(zifu1);
    	zifu1.a1='d';
    	zifu1.a2='e';
    	zifu1.a3='f';
    	zifu1.b1=2;
    	a.push_back(zifu1);
    	for(vector<zifu>::const_iterator iter = a.begin(); iter != a.end(); ++iter)
    	{
    		cout<<*iter<<endl;
    	}
    
    	// Write File
    	ofstream file;
    	file.open("E:\\11.txt");
    	char buf[32] = {0};
    	string strText;
    	for(vector<zifu>::const_iterator iter = a.begin(); iter != a.end(); ++iter)
    	{
    		memset(buf, 0, sizeof(buf));
    		sprintf(buf, "%d", iter->b1);
    		strText += iter->a1 + ", " + iter->a2 + ", " + iter->a3 + "," + buf + "\r\n";
    	}
    	file.write(strText.c_str(), strText.length());
    	file.close();
    	return 0;
    }
    


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2013年7月25日 7:49
    版主
  • 非常感谢!

    thank you very munch !

    2013年8月3日 0:17