我有以下结构.
struct ShaderObject
{
const GLchar* File;
ShaderType Type;
GLuint Shader;
ShaderObject(const GLchar* File, ShaderType Type);
};
Run Code Online (Sandbox Code Playgroud)
而这种类型的地图.
typedef map<string, ShaderObject> Shaders;
Run Code Online (Sandbox Code Playgroud)
编译器给我一个错误,链接到执行map.
Error 1 error C2512: 'ShaderObject::ShaderObject': No appropriate default constructor
available c:\program files (x86)\microsoft visual studio 11.0\vc\include\map 198 1
Run Code Online (Sandbox Code Playgroud)
我不明白这个错误.我该如何解决?因为我以前从来没有这样的错误,我无法解释,我不确定你需要哪些信息.请随时询问详情!非常感谢!
问题在stdmap中使用char作为键,建议使用自定义比较函数/ 仿函数:
struct cmp_str
{
bool operator()(char const *a, char const *b)
{
return std::strcmp(a, b) < 0;
}
};
map<char *, int, cmp_str> BlahBlah;
Run Code Online (Sandbox Code Playgroud)
这允许map检测键A是否小于键B.但是例如 map <> :: find()如果找不到元素则返回end,如果找到则返回iterator.因此,地图知道等价,而不仅仅是低于.怎么样?
我不知道它通常是怎么称呼的,但我需要的是这样的:
f a b c = a + 2 * b + 3 * c
map f [(1,2,3), (4,5,6), (7,8,9)]
Run Code Online (Sandbox Code Playgroud)
即将n元组的列表映射到具有n个单独参数的函数.在Haskell中有没有内置的方法可以做到这一点?
PS:我uncurry刚刚发现,但它似乎没有用3个参数这样做,只有2个.
基本上,我需要找到所有匹配的字谜到一个单词.我正在做的是使用大小为26的数组来表示单词中的字母.例如:abcdefg = {1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0} aaaaaaa = {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0}
这就是我创建数组的方式.
//stringtemp is a C++ string representing the word.
//letters is a size 26 int array representing all the letters in the string.
for(int i=0;i<stringtemp.length();i++)
{
letters[stringtemp[i]-65]+=1;
}
Run Code Online (Sandbox Code Playgroud)
这就是我将数组存储在地图中的方式.
dictionary[letters].push_back(stringtemp);
Run Code Online (Sandbox Code Playgroud)
所以,我做错了什么,或者在C++中这是不可能的.在我发现的所有其他答案中,他们建议使用矢量作为关键,但这在我的情况下不起作用(我想.)
我正在研究我的第一个Qt应用程序,小部件,当我尝试用一<int, QString>对填充标准库映射时,我得到了一个段错误.我的目标是用int键和QString值填充地图.我不知道对是否是最好的方法,所以任何建议都会很棒.
这是除main之外唯一的源文件.
#include "linuxtips.h"
#include "ui_linuxtips.h"
LinuxTips::LinuxTips(QWidget *parent) :
QWidget(parent),
ui(new Ui::LinuxTips)
{
loadRandTip();
ui->setupUi(this);
}
LinuxTips::~LinuxTips()
{
delete ui;
}
void LinuxTips::on_learnMore_clicked()
{
}
void LinuxTips::on_viewAll_clicked()
{
}
void LinuxTips::loadRandTip()
{
int i = 0;
std::map<int, QString>::iterator it;
QString line;
QFile inputFile(":/tipFile.txt");
inputFile.open(QIODevice::ReadOnly);
QTextStream in(&inputFile);
do{
line = in.readLine();
// this->TipMap.insert(it, std::pair<int, QString >(i,line));
i++;
}while(!in.atEnd());
}
Run Code Online (Sandbox Code Playgroud)
如果我取消注释this->TipMap.insert(it, std::pair<int, QString >(i,line));,它将运行.由于它是一个seg错误,我确定它是一个内存溢出或空指针,但我只是不确定它是什么.谢谢你的帮助.
我有以下7行代码,如何让它们更短,更少罗嗦?
max_group_size = 0
wrong_services.each do |service|
group_size = service.iep_service.group_size
if group_size > max_group_size then
max_group_size = group_size
end
end
Run Code Online (Sandbox Code Playgroud) 如果我有两张地图可以保证拥有完全相同的密钥集,那么如何才能有效地迭代两张地图呢?
例如,假设我有以下地图:
std::map<std::string, int> iMap;
std::map<std::string, std::vector<int> > vMap;
Run Code Online (Sandbox Code Playgroud)
在某些时候,他们最终都拥有完全相同的密钥集.我现在需要vMap根据相应的iMap值更新所有值.首先想到的是这样的事情:
typedef map<string, int> map_t;
BOOST_FOREACH(map_t::value_type &p, iMap) {
vMap[p.first].push_back(p.second);
}
Run Code Online (Sandbox Code Playgroud)
然而,vMap[n]考虑到我们有效地按顺序查看键,我们必须查找每个值,这似乎相当浪费.我们有什么方法可以利用这个吗?
我在SML中列出了N个元素。
我想将函数应用于该列表中的每个元素,因此我使用map。
但是,我要应用的函数具有多个这样的参数:
foo a b (c, d)
Run Code Online (Sandbox Code Playgroud)
其中a是我从列表中使用的元素,而bc和d是每次都相同的预定义变量。
我这样声明我的功能:
fun foo2 = map foo aList b (c,d)
Run Code Online (Sandbox Code Playgroud)
但是我遇到了一个运算符和操作数错误,这是预料之中的,但我可以想到任何其他方法来执行此操作。
这是我的结构,我正在尝试为此编写默认构造函数.
struct Cnode
{
typedef std::map<char, int> nextmap;
typedef std::map<char, int> prevmap;
Cnode() : nextmap(), prevmap() {} //error
Cnode(const nextmap2, const prevmap2) : nextmap(nextmap2), prevmap(prevmap2) {}
};
Run Code Online (Sandbox Code Playgroud)
请帮我理解这个错误意味着什么:
Type 'nextmap'(aka 'map<char,int>') is not a direct or virtualbase of 'Cnode'
Type 'prevmap'(aka 'map<char,int>') is not a direct or virtualbase of 'Cnode'
Run Code Online (Sandbox Code Playgroud) 我有一堆字符串,全部在一行上,由一个空格分隔.我想将这些值存储在地图中,第一个字符串作为键,以及一组剩余值.我在尝试
map = {}
input = raw_input().split()
map[input[0]] = input[1:-1]
Run Code Online (Sandbox Code Playgroud)
除了最后一个元素之外,它有效.我已经找到
map[input[0]] = input[1:len(input)]
Run Code Online (Sandbox Code Playgroud)
有效,但我宁愿使用更像前者的东西
(例如,输入类似于"键值1值2值3"我想要像
{'key':['value1','value2','value3']}这样的地图
但是我当前的方法给了我
{'key':[ 'value1','value2']})