我收到以下错误:
指向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) 我需要创建一个可以包含我的父类和子类数据的向量.
这就是我做的..
车辆是父类
汽车是儿童班
关于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) 这是代码:
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)
不知道从哪里开始寻找错误.
将向量分配给数组时,为什么我的大小会发生变化?
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)
我知道我可以循环并单独分配(而不是一行分配),但我只是想知道为什么会这样.
我正在尝试编写字符串列表.用户可以添加到列表或从列表中删除,以及显示当前列表.
显示列表和添加到列表工作正常,但我无法弄清楚如何通过遍历列表来查找匹配来删除用户的字符串.
我如何更改我的代码来解决这个问题? 看看是否(答案== 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) 我有一个函数,可以在向量中找到所有多个元素.如果我发送{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) 当我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,这不是问题的关键.
我想返回对作为参数的向量中的单个元素的引用,然后比较那些内存地址.我的代码的简化示例如下所示:
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)
如何返回对单个向量元素的引用?
我有一个包含ints 向量的向量对象
std::vector<std::vector<int>> vec;
Run Code Online (Sandbox Code Playgroud)
我一直在试图弄清楚它是如何std::sort(vec.begin(), vec.end())工作的。这是我的观察结果:
我现在已经生成了一些2D向量,看来这两个总是正确的。但是,我对第二个假设感到怀疑。难道std::sort真的以这种方式工作,或者它只是一些运气,使我的假设是否正确?
我试图擦除存储包含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)