在我的课堂上,当我尝试将任何对象推送到矢量myCache时,我收到运行时错误.我知道我正在正确地初始化矢量,并且很难理解为什么会发生这种情况.
#ifndef CACHE_H
#define CACHE_H
#include <iostream>
#include "cacheblock.h"
using namespace std;
class Cache
{
public:
Cache(int rows, int numWords);
~Cache();
CacheBlock getBlock(int index);
private:
vector<CacheBlock> *myCache;
};
#endif
Run Code Online (Sandbox Code Playgroud)
和
Cache::Cache(int rows, int numWords)
{
myCache = new vector<CacheBlock>;
CacheBlock test(rows, 0, 0);
myCache->push_back(test);
/*
for (int i = 1; i < rows; i++)
{
myCache->push_back(test);
cout << "inside loop\n\n";
}
*/
}
Run Code Online (Sandbox Code Playgroud)
CacheBlock.h:
class CacheBlock
{
public:
CacheBlock(int numWords, int newIndex, int tagNum);
CacheBlock(const CacheBlock &newCacheBlock);
~CacheBlock();
void setData(int …Run Code Online (Sandbox Code Playgroud) 除非我遗漏了某些东西或误解了机制(很可能)不应该在这个载体中不存在"1"重复?
chunks.erase( std::unique ( chunks.begin(), chunks.end(),
[]( std::string &s1, std::string &s2 ){
return ( s1.compare(s2) == 0 ? true : false );}),
chunks.end() );
Run Code Online (Sandbox Code Playgroud)
在执行上述之前:
1 l:1
1+ l:2
1+1 l:3
1+1= l:4
+ l:1
+1 l:2
+1= l:3
1 l:1
1= l:2
= l:1
Run Code Online (Sandbox Code Playgroud)
执行上面的代码后:
1 l:1
1+ l:2
1+1 l:3
1+1= l:4
+ l:1
+1 l:2
+1= l:3
1 l:1
1= l:2
= l:1
Run Code Online (Sandbox Code Playgroud)
我试过没有谓词(假设std ::相同的字符串将被删除).出于某种原因,"那些"被认为是相同的?我已经查看了它们的长度(假设空格被卡住作为前缀或后缀),但它们具有相同的长度.
我错过了什么吗?
我知道在C++ 11中我可以使用如下语法构造一个向量:
vector <int> a = {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud)
但是,如果没有以类似的方式循环来为多个相等元素初始化向量,它是否可能?
例如
int n= 5;
vector <string> a = (n, {"bbb"});
Run Code Online (Sandbox Code Playgroud) 我对C++比较陌生,我遇到了一个我无法解决的问题.我得到的确切错误是:
Pg1_raycast_nabrown3.exe中0x760d34c5的第一次机会异常:0xC0000005:访问冲突读取位置0x0e296575.Pg1_raycast_nabrown3.exe中0x760d34c5处的未处理异常:0xC0000005:访问冲突读取位置0x0e296575.
当我尝试访问矢量实例变量时会产生这些.这是代码:
group.h
class Group {
private:
string name;
vector<Face*> members;
Material* material;
public:
Group(string n);
~Group();
void addFace(Face* f);
string getName();
vector<Face*> getMembers();
void setMaterial(Material * mat);
Material* getMaterial();
};
Run Code Online (Sandbox Code Playgroud)
group.cpp
// Constructor
Group::Group(string n) {
name = n;
}
// Deconstructor
Group::~Group() {}
/**
* addFace
*
* adds the provided face to this group
*/
void Group::addFace(Face* f) {
// DEBUGGING
OutputDebugStringA("A\n");
OutputDebugStringA("A " + members.size());
members.resize(members.size() + 1);
OutputDebugStringA("B\n");
OutputDebugStringA("B " + members.size());
members.push_back(f);
} …Run Code Online (Sandbox Code Playgroud) 我正在使用擦除删除成语:
template <typename T>
bool garbageCollectVector(std::vector<T>& v) {
// Use the erase-remove idiom in combination with a lambda expression
v.erase(
std::remove_if(v.begin(), v.end(),
[this](const T& elem) -> bool {
return this->shouldRemove(elem);
}
),
v.end());
return /* what to return? */;
}
Run Code Online (Sandbox Code Playgroud)
并且想要返回该方法是否实际删除了任何元素.什么是干净办呀?
我正在尝试创建继承自std :: vector的MyVector类(添加一些有用的方法).一切都很好,但无法使用_initializer_list_进行初始化:
std::vector<int> a = { 4, 2 }; // OK
MyVector<int> b = { 4, 2 }; // Error
Run Code Online (Sandbox Code Playgroud)
VS2015和gcc都不允许编译它:
error: could not convert '{2, 3, 4}' from '<brace-enclosed initializer list>' to 'MyVector<int>'
Run Code Online (Sandbox Code Playgroud)
那又怎样?我尝试使用_initializer_list_ param明确添加构造函数来解决问题(参见下面的代码),但为什么呢?为什么它不继承自std:vector?
template <class T>
class MyVector : public std::vector<T>
{
public:
// Why is this constructor needed???
MyVector(const std::initializer_list<T>& il)
: std::vector<T>(il)
{
}
};
Run Code Online (Sandbox Code Playgroud)
PS我不想添加这个构造函数,以避免编写任何其他构造函数...
我试图在向量中插入一个元素,但似乎我做错了.
这是我声明向量的地方:
std::vector<Dice> dicearray;
这是抛出错误的行:
dicearray.insert(dicearray.size()-1 ,Dice());
这些是抛出的错误:
Error (active) no instance of overloaded function "std::vector<_Ty, _Alloc>::insert [with _Ty=Dice, _Alloc=std::allocator<Dice>]" matches the argument list ConsoleApplication3 c:\Users\Miguel\Documents\Visual Studio 2015\Projects\ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.cpp 60
和
Error C2664 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Dice>>> std::vector<Dice,std::allocator<_Ty>>::insert(std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<Dice>>>,unsigned int,const _Ty &)': cannot convert argument 1 from 'unsigned int' to 'std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<Dice>>>' ConsoleApplication3 c:\users\miguel\documents\visual studio 2015\projects\consoleapplication3\consoleapplication3\consoleapplication3.cpp 60
知道为什么会这样吗?
我希望有一个包含向量的节点数据结构.我SIZE事先知道了向量的大小,因此我使用const说明符初始化变量.
vectorTest.cpp):#include <vector>
const int SIZE = 100;
struct node {
std::vector<int> myVec(SIZE); // ERROR: 'SIZE' is not a type
};
int main(int argc, char const *argv[]) {
std::vector<int> myVec(SIZE); // VALID
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g++ 5.4.0):g++ -std=c++1y vectorTest.cpp -o vectorTest
Run Code Online (Sandbox Code Playgroud)
在里面main(),一切都很好,我可以高兴地宣布:std::vector<int> A(SIZE);.但是,当我尝试在a中定义相同的内容时struct,我会收到错误消息'SIZE' is not a type.
我知道我可以这样做来int使用C风格的数组定义来声明s 的向量,
struct other_node {
int myVec[SIZE]; // VALID
};
Run Code Online (Sandbox Code Playgroud)
但我想知道为什么这是不可能的std::vector. …
(1)如何创建一个std :: vector函数,以便您可以执行以下操作:
int main ()
{
std::vector<????> vector_of_functions;
// Add an adding function into the vector
vector_of_functions.push_back(
double function (double a, double b) {
return a + b
}
);
// Add a multiplying function into the vector
vector_of_functions.push_back(
double function (double a, double b) {
return a * b;
}
);
// Use the functions
std::cout << "5 + 7 = " << vector_of_functions[0](5, 7); // >>> 5 + 7 = 12
std::cout << "5 * 7 = …Run Code Online (Sandbox Code Playgroud) 我是C++ 11的新手,我对std::refence_wrapper课程的使用感到困惑.
让我们考虑一下https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper中显示的示例
,它会混淆标准中包含的元素std::vector,即:
std::vector<int> l(10);
std::iota(l.begin(), l.end(), -4);
std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
Run Code Online (Sandbox Code Playgroud)
现在,如果考虑原始向量l,我会l.data()返回一个指向内部数组的指针,我可以在C应用程序中使用它.
相反,我不清楚返回的是什么v.data().我尝试了各种肯定错误的转换的组合,例如int* p = (int*)(v.data()->get())没有获得正确的结果(交换的值).
我的目标是将C++应用程序(它给我一个引用包装器的向量)与旧的C库连接起来.v在示例中显示的shuffle之后,你能指出我从向量中获取类C数组的最正确方法吗?我是否需要在另一个向量中逐个复制所有元素?有没有可以帮助我完成工作的C++ 11课程?谢谢