English 中文(简体)
c++: 参考地图
原标题:c++: Reference array of maps

我的职能是制作一系列地图:

map<string, int> *pMap

a) 向阵列撰写地图的功能:

int iAddMap(map<string, int> mapToAdd, map<string, int> *m, int i)
{
    m = &(pMap[i]);
    memcpy(m, mapToAdd, sizeof(map<string, int>));
}

从阵列获取地图的功能

map<string, int>& getMap(int i)
{
    return pMap[i];
}

我可以毫无问题地向阵列绘制地图,但每一个地方的电话都会导致eg误:

int val; 
// val defined after this
map<string, int> * pGetMap = &(getMap(val));

任何关于为何发生这种情况的建议?

最佳回答

您不能使用薄膜复制像地图或实际上任何其他种类的C++集装箱等物体——你们需要使用转让,而转让应考虑到地图的基本结构和特性。 你们应当使用<条码>查询器和带;图和特;,因为你实际上似乎想要的是复制件而不是点子。

问题回答

从来没有使用<代码>memcpy复制地图标的(或该物体的任何类别)——使用转让或复印件。 <代码>memcpy是一种C功能,对C++物体的内部结构没有任何想法,因此几乎可以保证对东西方进行保。

并且,你将更好地利用<条码>,代之以阵列:

std::vector<map<string, int> > maps;

void addMap(const map<string, int>& mapToAdd, int i)
{
    maps[i] = mapToAdd;
}

map<string, int>& getMap(int i)
{
    return maps[i];
}

注:addMap在你的榜样代码中不退还任何款项,因此我将其返回类型改为void/code>。 我也通过<代码>ToAdd作为参考资料而不是按价值计算。

这似乎像C和C++的方案拟订工作一样,从变化开始:

memcpy(m, mapToAdd, sizeof(map<string, int>));

纽约总部

*m = *mapToAdd;

在职能方面

int iAddMap(map<string, int> mapToAdd, map<string, int> *m, int i)

What is the purpose of m? Any modifications 纽约总部 m won t be seen outside of this function.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签