免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2329 | 回复: 9
打印 上一主题 下一主题

[C] vector怎么使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-27 15:20 |只看该作者 |倒序浏览
#include<iostream>
#include<string>
#include<vector>
using namespace std;
typedef struct test
{
   int a;
   int b;
};
typedef struct test1
{
   int a1;
   int b1;
}test2[2];
int main()
{

   vector<test*> _test;

    test1 test2[2] = {{1,2},{2,3}};


    int i;
    for(i=0;i<2;i++)
   {
       _test.push_back(test2[i]);
   }

    for(i=0;i<2;i++)
    {
       cout << _test[i].b<<endl;
    }
    return 0;
}

想把test2值赋给_test,但是这样报错,vector怎么用啊

论坛徽章:
0
2 [报告]
发表于 2011-01-27 15:32 |只看该作者
本帖最后由 teng0210 于 2011-01-27 15:34 编辑

_test.push_back((test *) & test2);
//编辑器把后面的i弄没了,无语...


for(i=0;i<2;i++)
    {
       cout << _test->b<<endl;
    }

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
3 [报告]
发表于 2011-01-27 15:39 |只看该作者
_test.push_back((test *) & test2);
//编辑器把后面的i弄没了,无语...


for(i=0;i
teng0210 发表于 2011-01-27 15:32



    不会用code方式贴代码,无语...

论坛徽章:
0
4 [报告]
发表于 2011-01-27 16:04 |只看该作者
{:3_186:} sl在说什么...

怎么用 去看书吧 比较详细...
有个 C-C++库函数.chm  上网上搜索下

论坛徽章:
0
5 [报告]
发表于 2011-01-27 16:04 |只看该作者
push_back还要加个类型转换?实践证明是需要加的

论坛徽章:
2
戌狗
日期:2013-11-06 17:35:36寅虎
日期:2014-10-20 23:12:29
6 [报告]
发表于 2011-01-27 16:25 |只看该作者
http://www.cppreference.com
这个网站很好

论坛徽章:
0
7 [报告]
发表于 2011-01-27 17:24 |只看该作者
内事不决问百度

论坛徽章:
0
8 [报告]
发表于 2011-01-27 17:30 |只看该作者
内事不决问老婆

论坛徽章:
0
9 [报告]
发表于 2011-01-28 20:29 |只看该作者
// testVector.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;

typedef struct test
{
   int a;
   int b;
};
typedef struct test1
{
   int a1;
   int b1;
}test1;


int main(int argc, char* argv[])
{
        vector<test*> _test;

    test1 test2[2] = {{100,2},{200,3}};

        test* newTest1;
        newTest1 = new test;
        test* newTest2;
        newTest2 = new test;
       
        //newTest1 = &test2[0];
        //newTest2 = &test2[1];
        memcpy( (char*)newTest1, (char*)&test2[0], sizeof(test) );
        memcpy((char*)newTest2, (char*)&(test2[1]), sizeof(test) );

    int i;
    for(i=0;i<2;i++)
        {
                //_test.push_back(test2[i]);
        }
      
           _test.push_back(newTest1);
           _test.push_back(newTest2);
   

    for(i=0;i<2;i++)
    {
           cout << _test[i]->a<<endl;
       cout << _test[i]->b<<endl;
    }

        cin >> i;

        return 0;
}

论坛徽章:
0
10 [报告]
发表于 2011-01-28 20:38 |只看该作者
最后加上   delete newTest1;
        delete newTest2;
vc 6.0中
测试可用
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP