我正在为一个我正在研究的游戏编辑一个黑客,作为该编辑器的一部分,显然我需要有纹理.我已经创建了一个std :: map变量,
std::map<std::string, unsigned int> textures;
Run Code Online (Sandbox Code Playgroud)
在我的图片加载代码中,我有以下代码段.
unsigned int id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
glBindTexture(GL_TEXTURE_2D, 0);
textures[filename] = id;
Run Code Online (Sandbox Code Playgroud)
现在由于某种原因,我在尝试使用上面的代码后出现运行时错误.一个访问冲突错误,在调试时,我指向std :: map代码本身,具体来说,这部分:
_Nodeptr _Lbound(const key_type& _Keyval) const
{ // find leftmost node not less than _Keyval
_Nodeptr _Pnode = _Root(); // ** this is the highlighted line **
_Nodeptr _Wherenode = _Myhead; // end() if search fails
while (!_Isnil(_Pnode))
if (_DEBUG_LT_PRED(this->comp, _Key(_Pnode), _Keyval))
_Pnode …Run Code Online (Sandbox Code Playgroud) 我试图用g ++ 4.4编译并链接一个使用STL的简单程序.我正在尝试使用-fno-implicit-templates来完成它,因此必须显式实例化所有模板.
我不明白为什么这段代码有效:
#include <map>
//template class std::map<char,char>;
template class std::_Rb_tree<char, std::pair <char const, char>,
std::_Select1st<std::pair<char const, char> >,
std::less<char>, std::allocator<std::pair<char const, char> > >;
int main() {
std::map <char,char> table;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望这个程序需要行:template class std::map<char,char>;但是该行不会使程序链接.这std::_Rb_tree line是必要的.为什么?
在此先感谢,任何提示将不胜感激.
我有这样一张地图:
std::map<time_t, int>
Run Code Online (Sandbox Code Playgroud)
每天 (time_t) 有一个值 (int)。有些日子可能具有相同的值,因此可能不是唯一的。我需要为此地图中的每个唯一 int 值执行计算。
检索它们的最快(最少 CPU 使用率)方法是什么?
我面临着std :: map的问题。出于未知原因,有时插入映射会导致“分配错误”异常。
以下是我用于插入地图的函数。
BOOL Add2WaitList(Object<LPVOID> *newObj)
{
try
{
_set_se_translator( trans_func );
m_syncWQ.Lock();
if (m_waitingQueue.count(newObj->uid)>0)
{
m_syncWQ.Unlock();
return FALSE;
}
m_waitingQueue[newObj->uid] = *newObj; <-- failing here
m_syncWQ.Unlock();
return TRUE;
}
catch(std::exception &ex){
...
}
catch(SE_Exception &e){
...
}
catch(...){
...
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何解决吗?
注意:我无法确定重现它的步骤。
提前THX!
添加有关对象和地图的详细信息:
template <typename T>
struct Object{
public:
void Kill()
{
if (response!=NULL)
delete response;
if (object!=NULL)
delete object;
}
enum objType;
std::string uid;
enum status;
double p;
enum execType;
T object;
LPVOID response;
}; …Run Code Online (Sandbox Code Playgroud) 我在这里遇到并发问题.我有一个std::map,有一个偶尔的作家和来自不同线程的多个频繁读者,这个作者偶尔会std::string在地图上添加键(键是a ),我无法保证读者何时执行阅读和停止阅读.我不想为读者设置锁,因为阅读非常频繁并经常检查锁会损害性能.
如果读者总是通过键(而不是map迭代器)访问映射,它是否总是线程安全的?如果没有,任何想法如何设计代码,以便读者总是访问有效的键(或map迭代器)?
使用不同容器解决该问题的其他方法也是受欢迎的.
我想要从beginIt到endIt擦除std :: map的元素.erase函数将迭代器返回到删除的最后一个元素后面的元素.不是它的结束吗?为什么擦除返回迭代器?
auto it = m_map.erase(beginIt, endIt);
Run Code Online (Sandbox Code Playgroud) 以下 C++ 代码行给出了运行时错误,但如果删除了擦除操作,mymap.erase(v)则它可以工作:
map<int,int> mymap = {{1,0},{2,1},{9,2},{10,3},{11,4}};
for(auto it=mymap.rbegin();it!=mymap.rend();){
int v=it->first;
++it;
mymap.erase(v);
}
Run Code Online (Sandbox Code Playgroud)
这里迭代器it在删除它的 value 之前被改变v,所以it我相信迭代器应该不受影响。
我希望std::map标头中的 const 作为将在其他 cpp-s 中使用的全局常量。所以我将其声明为:
// header.h
const inline std::map<int, int> GlobalMap = { {1, 2}, {3, 4} };
Run Code Online (Sandbox Code Playgroud)
但是,如果我在多个 cpp-s 中包含此标头,则在退出时会发生堆损坏,因为为同一内存地址运行多个析构函数。
我一直认为内联 const 是全局非文字常量的灵丹妙药。我已将 global std::string-s 声明为const内联,并且效果很好。
所以我的问题是:
const内联不是很容易出错吗?const std::map在 C++17 中正确声明全局?我怎样才能确保只创建一个全局对象?编辑:我可以在 Visual Studio 2017 中的以下项目中重现它(/std:c++17,调试 x86)
文件_1.h:
#pragma once
#include <map>
const inline std::map<int, double> GlobalMap = {{1, 1.5}, {2, 2.5}, {3, 3.5}};
void f1();
Run Code Online (Sandbox Code Playgroud)
文件_1.cpp:
#include "file_1.h"
void f1()
{
(void)GlobalMap;
}
Run Code Online (Sandbox Code Playgroud)
主要.cpp:
#include "file_1.h"
int …Run Code Online (Sandbox Code Playgroud) std::map<int, Obj> mp;
// insert elements into mp
// case 1
std::map<int, Obj> mp2;
mp2 = std::move(mp);
// case 2
std::map<int, Obj> mp3;
std::move(std::begin(mp), std::end(mp), std::inserter(mp3, std::end(mp3));
Run Code Online (Sandbox Code Playgroud)
我对这两个案例感到困惑。它们完全一样吗?
我注意到下面示例中的使用std::map::reverse_iterator不适用于 C++20,但适用于所有编译器中的 C++17。
#include <map>
class C; //incomplete type
class Something
{
//THIS WORKS IN C++17 as well as C++20 in all compilers
std::map<int, C>::iterator obj1;
//THIS DOESN'T WORK in C++20 in all compilers but works in C++17 in all compilers
std::map<int, C>::reverse_iterator obj2;
};
int main()
{
Something s;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是so(such)发生了什么变化,导致所有编译器中的C++20使用停止工作。std::map::reverse_iteratorC++20