作为向量的一部分,使用 [] 我无法初始化。
std::vector<int> vectorVar;
vectorVar[0] = 0;
Run Code Online (Sandbox Code Playgroud)
看到未定义的行为。
而只有在已经创建的情况下才能使用。
vectorVar.push_back(0);
vectorVar[0] = 5;
Run Code Online (Sandbox Code Playgroud)
但如果有地图,即使没有条目也可以使用。
std::map<int, std::vector<int>> mapVar;
mapVar[0] = vectorVar;
Run Code Online (Sandbox Code Playgroud)
请澄清。
感谢和问候 毗湿奴比玛
如果“vectorVar[0] = 0;”,则会出现未定义的行为
我想我不明白 的初始化std::vector。
下面的代码似乎可以使用 gcc、clang 和 MSVC 编译并正常运行。
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{{}};
v[10] = 11; // if initialize with v{}, segmentation fault.
std::cout << "v[10]:" << v[10] << std::endl; // prints v[10]:11
for (auto e : v) // prints only v[x]:0
std::cout << "v[x]:" << e << std::endl;
}
// result:
// v[10]:11
// v[x]:0
Run Code Online (Sandbox Code Playgroud)
访问元素是否会v[10]导致未定义的行为?
为什么上面的代码用v{{}}可以正常运行,但用 则不行v{}?
编辑)最初我没有使用正确的术语,我的错。当我说“未编译”时,我想说的是“未运行(编译后)”。
注意:请不要仅仅因为根本原因是逗号运算符而关闭问题。这个问题的价值在于让社区了解以下方面的失败:
std::vector<std::pair<int, int>> vpairs_third{(0, 1), (0, -1)};
Run Code Online (Sandbox Code Playgroud)
是由于逗号运算符造成的。
我正在列表初始化一个包含整数对的向量。
我按照预期理解以下作品:
std::vector<std::pair<int, int>> vpairs_first{std::make_pair(0, 1), std::make_pair(0, -1)};
Run Code Online (Sandbox Code Playgroud)
std::vector<std::pair<int, int>> vpairs_second{{0, 1}, {0, -1}};
Run Code Online (Sandbox Code Playgroud)
但是,有人会解释为什么以下不起作用吗?
std::vector<std::pair<int, int>> vpairs_third{(0, 1), (0, -1)};
Run Code Online (Sandbox Code Playgroud) 对于这段代码,我使用data()向量的方法来访问其元素:
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (5);
int* p = myvector.data();
*p = 10;
++p;
*p = 20;
p[2] = 100;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会产生
myvector contains: 10 20 0 100 0
Run Code Online (Sandbox Code Playgroud)
问题是为什么p[2]是0?假设myvector.data()是一个指向第一个元素(索引 0)的指针,那么p[0]就是 10 就可以了。++p指向下一个元素,即p[1]20,这也很好。现在,p[2]是 后的下一个元素p[1]。所以,顺序应该是10 20 100。这里有什么错误吗?
存在此C++对象的两个实例.
my_type
{
public:
std::vector<unsigned short> a;
}
Run Code Online (Sandbox Code Playgroud)
std :: vector为空,另一个包含50个元素.
哪个实例复制最快或者同时复制?
我有一个向量,其中包含一个类对象.我是这样宣布的
vector<Aircraft> queue;
Run Code Online (Sandbox Code Playgroud)
我擦除了向量中的第一个元素.但它似乎仍然是向量中的第一个元素.我这样做了
queue.erase(queue->begin());
cout<<queue.size(); //printed 0
Aircraft temp = queue.front();
cout<<temp.id; //expected a segfault error
Run Code Online (Sandbox Code Playgroud)
擦除第一个元素后,向量的大小为0(只有一个元素).当我试图查看temp(飞机)的id时,我期待一个seg错误错误,因为我从矢量中删除了第一个元素,之后大小为零.我甚至清除了向量,但'queue.front '仍然返回对象,而cout语句仍然打印对象的id.
我似乎无法管理访问向量元素的语法,其指针包含在结构中.更多MWE之后:
#include <vector>
#include <stdio.h>
typedef struct vectag
{
std::vector<float> *X;
} vec;
int main ()
{
vec A;
A.X = new std::vector<float>(0);
A.X->push_back(5.0);
// This next line is the problem:
float C = A.X[0];
printf("%f\n", C);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
GCC(G ++)说
14:24: error: cannot convert ‘std::vector<float>’ to ‘float’ in initialization
Run Code Online (Sandbox Code Playgroud)
当然,这是非常正确的.在该行中float C = A.X[0];,BX [0]将是正确的,如果X不是指针(召回std::vector<float> *X;).在operator []之前解除引用X的正确语法是什么,以便我可以访问X的元素?
PS我知道成员函数at(),它不是我的选项,因为我不想要范围检查的开销.这是性能关键代码的一部分.
我有一个
class myclass
{
// ...
static const vector<pair<string,vector<string>>> var;
// ...
};
Run Code Online (Sandbox Code Playgroud)
在类定义中,使用单个字符串到其他几个字符串的映射.我使用vector <>而不是数组,以便能够添加映射对和映射长度,而不必使用大小变量.有没有办法在相应的.cpp文件中初始化变量,就像非复合类型的向量一样,即格式为:
const vector<pair<string,vector<string>>> myclass :: var =
{
???
}
Run Code Online (Sandbox Code Playgroud)
或者我必须使用静态方法,如
static myclass::initStaticMembers(){...}
Run Code Online (Sandbox Code Playgroud)
如果有第一种方法可以做到这一点,那么语法是什么?我搜索过,但没有找到复合std :: pair初始化的语法.例如,你可以初始化一个vector<string>与
vector <string>myvec={"elem1", "elem2","elem3"};
Run Code Online (Sandbox Code Playgroud)
但你怎么开始复杂的vector<pair<string,vector<string>>>?谢谢.
我正在尝试[]在向量上重载运算符.我试图让这个运算符在Matlab或Python中工作,用于负索引或大于矢量长度的索引.我的问题不是得到正确的结果,而是实际重载运算符,知道我将在重载代码中使用非重载运算符.我需要它用于特定类的向量,但如果它适用于任何类型它会更好vector.
现在我的代码是:header:
MyClass std::vector::operator[](std::vector<MyClass> const vector,int const idx) const;
Run Code Online (Sandbox Code Playgroud)
资源:
Myclass vector::operator[](vector<MyClass> const vector,int const idx) const {
n=vector.size()
if((idx>=0) && (idx<n)){
return vector[idx];
}
else if(idx<0){
return vector[n+idx%n]
}
else{
return vector[idx%n]
}
}
Run Code Online (Sandbox Code Playgroud)
我得到错误:
error: 'template<class _Tp, class _Alloc> class std::vector' used without template parameters
error: non-member function 'MyClass operator[](std::vector<MyClass>, int)' cannot have cv-qualifier
error: 'MyClass operator[](std::vector<MyClass>, int)' must be a nonstatic member function
Run Code Online (Sandbox Code Playgroud)
如果这个问题已经讨论过了,我很抱歉,但是我找不到它......非常感谢你提前得到答案!
我正在尝试使用具有相同基类的不同子类指针的向量。该向量设置为基类指针,但是添加到该向量的任何内容都无法获得其子类的全部功能。在错误日志中可以看到,它被视为基类,因此未获得扩展功能。
我查看了很多问题,人们说要按照我的方式去做,但是由于某种原因,它没有用。
该代码位于公共仓库中:
https://repl.it/@cubingminer8/inheritance-with-vectors-testing
任何帮助将不胜感激!
编辑:确定,所以我将在c ++ sdl2游戏引擎中将其用于子画面组系统。将有一个基本的Sprite类,其中包含一些基本的东西,例如render和move,而我需要的任何sprite都是它们自己的继承自Sprite的类,它们将具有自己的独特行为,因此虚函数是不切实际的。将有一个sprite组对象,可以将继承自Sprite的对象存储在其中。这样就可以一次全部渲染它们。
如果您曾经使用过pygame,那么它几乎与那里使用的sprite和spritegroup系统相同。 https://www.pygame.org/docs/tut/SpriteIntro.html
#include <iostream>
#include <vector>
class Base {
public:
char A = 'A';
};
class Sub : public Base {
public:
char B = 'B';
};
class Sub2 : public Base {
public:
char C = 'C';
};
int main() {
std::vector<Base*> List;
List.push_back(new Sub());
List.push_back(new Sub2());
std::cout << List[0]->B << std::endl; // it should be able to print B
std::cout << List[1]->C << std::endl; // but it is …Run Code Online (Sandbox Code Playgroud) c++ ×10
stdvector ×10
std-pair ×2
c++11 ×1
class ×1
composite ×1
dereference ×1
inheritance ×1
operators ×1
overloading ×1
performance ×1
pointers ×1
polymorphism ×1
stl ×1
vector ×1