标签: stdvector

使用std :: vector时访问冲突

我收到以下错误:

  • TBG.exe中0x012a4bd9处的未处理异常:0xC0000005:访问冲突读取位置0x0000002c.

指向vector.h的size()方法.使用此方法时似乎会发生:

void Player::printInventory(){
    if(inventory.size() != 0){
        for(unsigned int i=0; i<inventory.size(); i++){
            cout<<inventory[i] << endl;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

完整代码:

Player.h:

#pragma once
#include <vector>
#include <memory>

using namespace std;

class Player
{
private:
    int health;
    string name;
    vector<int> inventory;

public:
    Player(void);
    Player(string);
    ~Player(void);
    void changeHealth(int);
    void addToInventory(int);
    void removeFromInventory(int);
    void printInventory();
};
Run Code Online (Sandbox Code Playgroud)

Player.cpp:

#include "Player.h"
#include <iostream>
#include <string.h>

Player::Player(void)
{
    health = 20;
}

Player::Player(string newName)
{
    name = newName;
    health = 20;
}

Player::~Player(void)
{
}

void …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector

0
推荐指数
1
解决办法
3593
查看次数

父类和子类的std :: vector

我需要创建一个可以包含我的父类和子类数据的向量.

这就是我做的..

车辆是父类

汽车是儿童班

关于Car.cpp,它得到了以下内容

struct Point
{
    int x,y
};

class Car : public Vehicle
{
private:
    Point wheelPoint[4];
    double area;

public:
    void setPoint();
};

void Car::setPoint()
{
    int xData,yData;

    cout << "Please enter X:";
    cin >> xData;

    cout << "Please enter Y:";
    cin >> yData;

    wheelPoint[i].x = xData;
    wheelPoint[i].y = yData;
}
Run Code Online (Sandbox Code Playgroud)

然后在我的main.cpp

在我的main.cpp

vector<VehicleTwoD> list;
VehicleTwoD *vehicle;
Car *car = new Car;
string vehicleName;

cout << "Please input name of vehicle";
cin >> vehicleName;

vehicle = new …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector

0
推荐指数
1
解决办法
4329
查看次数

std :: vector中的意外更改

这是代码:

typedef struct Triplet
{
    double value;
    int row;
    int column;
};

class Matrix
{
public:
//Some methods
double specialMethod(/*params*/);
private:
    std::vector<Triplet> elements;
    int nRows;
    int nColumns;
};
Run Code Online (Sandbox Code Playgroud)

specialMethod被调用之后,Matrix.element中的值被破坏.但除了像这样迭代之外,没有任何事情可以做到:

std::vector<Triplet>::iterator it;
std::vector<Pair> buff;
for (it = this->elements.begin(); it != this->elements.end(); ++it)
    {

        if (it->column = n)
        { 
            Pair p;
            p.key = it->row;
            p.value = it->value;
            buff.push_back(p);
        }
    }
Run Code Online (Sandbox Code Playgroud)

不知道从哪里开始寻找错误.

c++ struct stdvector

0
推荐指数
1
解决办法
87
查看次数

C++将向量分配给数组

将向量分配给数组时,为什么我的大小会发生变化?

cout << endl << vertices.size() << endl;  //outputs correct number-> 89473
GLfloat* Vertices = &vertices[0];
GLfloat* Colors = &colors[0];
cout << endl << sizeof(Vertices) << endl; //outputs incorrect number-> 4
Run Code Online (Sandbox Code Playgroud)

我知道我可以循环并单独分配(而不是一行分配),但我只是想知道为什么会这样.

c++ arrays syntax stdvector

0
推荐指数
1
解决办法
82
查看次数

C++迭代向量并删除匹配的字符串

我正在尝试编写字符串列表.用户可以添加到列表或从列表中删除,以及显示当前列表.

显示列表和添加到列表工作正常,但我无法弄清楚如何通过遍历列表来查找匹配来删除用户的字符串.

我如何更改我的代码来解决这个问题? 看看是否(答案== 3)

// InClassAssignment-FavouriteGameList.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    vector <string>  gameList;
    int answer = 0;
    bool cont = true;
    int size = 0;
    vector<string>::iterator iter;
    string addToList;
    string removeFromList;

    cout << "\tGame List" << endl;

    while (cont)
    {
        cout << "--------------------------------------------" << endl;
        cout << "\nWhat …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stdvector

0
推荐指数
1
解决办法
755
查看次数

如何使用goto跳转到for循环的末尾,但不要离开它

我有一个函数,可以在向量中找到所有多个元素.如果我发送{1,2,3,4,5,1,2,3,3,7}它返回{1,2,3}.我的输入向量有大约100到10000个元素,但我希望只有很少的不同(!)重复; 约1-5%.

因此,如果我已经多次识别出一个元素,我会检查我的重复矢量.如果是,则该函数将继续执行下一个元素(如果有).为此我用了一个goto.

但是我需要在之后有一个命令goto label.否则编译器会抱怨.有没有办法避免这个并保持goto?我知道我可以使用其他方法,例如相应地设置bool并使用if().不过我认为goto方法很简单.

vector<int> findDublicates(vector<int> const& v) {
    // e.g. {1,2,3,4,5,1,2,3,7} -> {1,2,3}
    vector<int> dublicates;
    for (auto it(v.begin()); it != v.end() - 1;
         ++it) { // go through each element except the last
        for (auto const& i :
             dublicates) { // check if this is already a known dublicate
            if (i == *it)
                goto nextElement; // if so, goto the next element in v
        }
        for (auto it2(it + 1); …
Run Code Online (Sandbox Code Playgroud)

c++ goto stdvector

0
推荐指数
1
解决办法
227
查看次数

按容量迭代std :: vector

当我std::vector提前知道最终大小时,我通常会提前保留其容量以避免重新分配其内容(在C++ 03上涉及为已存储在其中的每个对象调用复制构造函数vector).

这是一个简单的例子:

std::vector<std::string> v;
v.reserve(10);    

for( std::vector<std::string>::size_type i = 0, capacity = v.capacity();
     i < capacity;
     ++i )
{
    v.push_back(std::to_string(i));
}
Run Code Online (Sandbox Code Playgroud)

循环std::vector容量有更好的方式(更少的C风格)?

我正在寻找C++ 03和C++ 11的答案.

编辑:我重写了样本,因为所有的答案和评论都是关于主题的,只关于用数组填充std :: vector,这不是问题的关键.

c++ stdvector

0
推荐指数
1
解决办法
136
查看次数

C++ - 返回对向量元素的引用

我想返回对作为参数的向量中的单个元素的引用,然后比较那些内存地址.我的代码的简化示例如下所示:

const int &test(std::vector<int> i) {
    return i.at(3);
}

TEST(test, all_test) {
    const std::vector<int> i = {1,2,3,4,5};
    const int &j = test(i);
    ASSERT_THAT(&j, Eq(&i.at(3)));
}
Run Code Online (Sandbox Code Playgroud)

使用此我收到以下错误消息:

Failure
Value of: &j
Expected: is equal to 0x55da7899d4dc
  Actual: 0x55da7899d4fc (of type int const*)
Run Code Online (Sandbox Code Playgroud)

如何返回对单个向量元素的引用?

c++ return reference stdvector

0
推荐指数
1
解决办法
600
查看次数

[C ++] [std :: sort]它如何在2D容器上工作?

我有一个包含ints 向量的向量对象

std::vector<std::vector<int>> vec;
Run Code Online (Sandbox Code Playgroud)

我一直在试图弄清楚它是如何std::sort(vec.begin(), vec.end())工作的。这是我的观察结果:

  1. 2D向量按大小排序。
  2. 如果某些内部向量具有相同的大小,则第一个元素的值较小的向量将具有较小的索引值。

我现在已经生成了一些2D向量,看来这两个总是正确的。但是,我对第二个假设感到怀疑。难道std::sort真的以这种方式工作,或者它只是一些运气,使我的假设是否正确?

c++ sorting stdvector

0
推荐指数
1
解决办法
67
查看次数

为什么vector :: erase无法在具有const的类元素上工作

我试图擦除存储包含const成员的类的向量的元素,但失败并出现错误。

当我删除常量修饰符时,程序将正常编译。

class C
{
public:
    const int i;
    C(int i = 1):i(i){};
};


int main()
{
    vector<C> v;
    C c;
    v.push_back(c);
    v.erase(v.begin());
    return 0;
}

error C2280: 'C &C::operator =(const C &)': attempting to reference a deleted function
m.cpp(151): note: compiler has generated 'C::operator =' here
m.cpp(151): note: 'C &C::operator =(const C &)': function was implicitly deleted because 'C' has a data member 'C::i' of const-qualified non-class type
m.cpp(146): note: see declaration of 'C::i'
Run Code Online (Sandbox Code Playgroud)

c++ stdvector

0
推荐指数
1
解决办法
65
查看次数

标签 统计

c++ ×10

stdvector ×10

arrays ×1

goto ×1

iterator ×1

reference ×1

return ×1

sorting ×1

struct ×1

syntax ×1