标题:这个为什么会报错?(关于vector的问题)
只看楼主
a74692074
Rank: 1
等 级:等待验证会员
帖 子:7
专家分:0
注 册:2018-7-19
结帖率:100%
已结贴  问题点数:20 回复次数:11 
这个为什么会报错?(关于vector的问题)
#include <iostream>
#include <vector>
#include <array>
using namespace std;

int main()
{
    double a1[4] = {1.2, 2.4, 3.6, 4.8};
    vector<double> a2(4);
    a2[0] = 1.0/3.0;
    a2[1] = 1.0/1.5;
    a2[2] = 1.0/7.0;
    a2[3] = 1.0/9.0;
    array<double, 4> a3 = {3.14, 2.72, 1.62, 1.41};
    array<double, 4> a4;
    a4 = a3;
    cout << "a1[2]:" << a1[2] << " at " << &a1[2] << endl;
    cout << "a2[2]:" << a2[2] << " at " << &a2[2] << endl;
    cout << "a3[2]:" << a3[2] << " at " << &a3[3] << endl;
    cout << "a4[2]:" << a4[2] << " at " << &a4[4] << endl;
    a1[-2] = 20.2;
    cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;
    cout << "a3[2]:" << a3[2] << " at " << &a3[2] << endl;
    cout << "a4[2]:" << a4[2] << " at " << &a4[2] << endl;
    return 0;
}
搜索更多相关主题的帖子: vector include array double cout 
2018-07-19 16:14
a74692074
Rank: 1
等 级:等待验证会员
帖 子:7
专家分:0
注 册:2018-7-19
得分:0 
有没有大神能帮帮忙??!
2018-07-19 16:15
mj0011
Rank: 2
等 级:论坛游民
帖 子:24
专家分:39
注 册:2014-12-23
得分:20 
arrary没有定义类型,
vector<double> arrary(4)这样写会不会更好一点
2018-07-19 16:19
a74692074
Rank: 1
等 级:等待验证会员
帖 子:7
专家分:0
注 册:2018-7-19
得分:0 
回复 3楼 mj0011
编译器还是报错
2018-07-19 16:21
mj0011
Rank: 2
等 级:论坛游民
帖 子:24
专家分:39
注 册:2014-12-23
得分:0 
回复 4楼 a74692074
你把改完的代码贴上来看一下
2018-07-19 16:23
a74692074
Rank: 1
等 级:等待验证会员
帖 子:7
专家分:0
注 册:2018-7-19
得分:0 
回复 5楼 mj0011
#include <iostream>
#include <vector>
#include <array>
using namespace std;

int main()
{
    double a1[4] = {1.2, 2.4, 3.6, 4.8};
    vector<double> array(4);
    a2[0] = 1.0/3.0;
    a2[1] = 1.0/1.5;
    a2[2] = 1.0/7.0;
    a2[3] = 1.0/9.0;
    array<double, 4> a3 = {3.14, 2.72, 1.62, 1.41};
    a4 = a3;
    cout << "a1[2]:" << a1[2] << " at " << &a1[2] << endl;
    cout << "a2[2]:" << a2[2] << " at " << &a2[2] << endl;
    cout << "a3[2]:" << a3[2] << " at " << &a3[3] << endl;
    cout << "a4[2]:" << a4[2] << " at " << &a4[4] << endl;
    a1[-2] = 20.2;
    cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;
    cout << "a3[2]:" << a3[2] << " at " << &a3[2] << endl;
    cout << "a4[2]:" << a4[2] << " at " << &a4[2] << endl;
    return 0;
}
2018-07-19 16:25
mj0011
Rank: 2
等 级:论坛游民
帖 子:24
专家分:39
注 册:2014-12-23
得分:0 
1.a2没有申明
2.vector对象不能通过下标来添加元素必须要用push_back()
2018-07-19 16:29
a74692074
Rank: 1
等 级:等待验证会员
帖 子:7
专家分:0
注 册:2018-7-19
得分:0 
回复 7楼 mj0011
没听懂你可以帮我修改一下吗?谢谢好人!
2018-07-19 16:30
mj0011
Rank: 2
等 级:论坛游民
帖 子:24
专家分:39
注 册:2014-12-23
得分:0 
回复 8楼 a74692074
using namespace std;

int main()
{
    double a1[4] = {1.2, 2.4, 3.6, 4.8};
    double tmp[4] = {3.0 ,1.5 ,7.0 ,9.0};
    vector<double> a4(4);
    std::vector<double> a2;

    for (size_t index = 0 ; index <4; ++index){
        a2.push_back(1.0 /tmp[index]);
    }

/*
    a2[0] = 1.0/3.0;
    a2[1] = 1.0/1.5;
    a2[2] = 1.0/7.0;
    a2[3] = 1.0/9.0;
    */
    vector<double> a3 = {3.14, 2.72, 1.62, 1.41};
    a4 = a3;
    cout << "a1[2]" << a1[2]<<"at " << &a1[2] << endl;
    /*cout << "a1[2]:" << a1[2] << " at " << a1[2] << endl;
    cout << "a2[2]:" << a2[2] << " at " << &a2[2] << endl;
    cout << "a3[2]:" << a3[2] << " at " << &a3[3] << endl;
    cout << "a4[2]:" << a4[2] << " at " << &a4[4] << endl;
    a1[-2] = 20.2;
    cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;
    cout << "a3[2]:" << a3[2] << " at " << &a3[2] << endl;
    cout << "a4[2]:" << a4[2] << " at " << &a4[2] << endl;
    */
    return 0;
}

[此贴子已经被作者于2018-7-19 17:14编辑过]

2018-07-19 17:03
mj0011
Rank: 2
等 级:论坛游民
帖 子:24
专家分:39
注 册:2014-12-23
得分:0 
回复 8楼 a74692074
下面一排cout估计你哪里输错了,我试着按你的原样在敲一遍是对的
2018-07-19 17:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-488695-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069614 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved