English 中文(简体)
stl:映射和镜像数据访问
原标题:stl:map and mirrored data access
  • 时间:2011-02-13 15:19:02
  •  标签:
  • c++

我正在从一个返回流速(双倍)和时间(双倍)的设备获取数据。我想存储它们,并能够使用流速来获取时间和时间来获取流速来访问数据。。。

我使用两个stl:map容器来完成此操作。。。有没有办法只使用一个容器?

Here is the method to load the data: Flow data (sData) is a comma delimited string ("11.2, 22.3, 14.3, 12.4, 13.3") Data is gathered each 0.25 seconds - so we just increment the time ...

void LiquidTest::Load(string sData)
{
  string sFlow;
  istringstream iss(sData);

  cout << "Inside LiquidTest::Load()." << endl;

  double dTime = 0.0;
  double dFlow = 0.0;

  while (getline(iss, sFlow,  , ))
  {
    // add the flow/time to the map(s)
    cout << "Adding flow/time to map. sFlow=" << sFlow << ", dTime=" << dTime << "." << endl;

    // Convert my string to a double
    std::stringstream s(sFlow);
    s >> dFlow;

    // add the flow data and time data to the maps. We will then 
    // be able to access the flow by the time key and the time 
    // by the flow key.  Do I need two maps ??? 
    m_mapFlowDataKeyTime.insert(pair<double, double>(dFlow, dTime));
    m_mapTimeKeyFlowData.insert(pair<double, double>(dTime, dFlow));

    // Increment the time
    dTime += 0.25;
  }
}
最佳回答

使用替身映射来搜索元素是一个问题,因为您无法期望替身进行准确的比较,而且它允许重复的可能性很小。

如果您真的知道数据中不会有重复,也不会有NaN值,并且您只想对值进行排序和范围比较,那么您可以继续使用map。

更可能的是,您想要的是结构或对的集合,以及以两种不同方式对它们进行排序的方法。Boost有多个索引,或者你可以通过设置排序标准轻松创建自己的索引。您可能希望将数据存储在一个条件中进行排序,然后在另一个条件上建立索引。

如果数据是静态的(即加载一次然后只搜索),那么维护起来就足够容易了。如果您不断添加新项目和删除项目,则需要更仔细的维护。

列表是一种存储方式,因为您可以对列表的迭代器进行索引,并且这些迭代器不会在稍后添加其他元素时失效。如果需要,在不使其他元素无效的情况下删除这些迭代器也很容易。

对于实际数据,你的时间只是0.25的步长,所以你实际上可以有一个表示流量的二重向量,你知道flow[i]中的时间是i/4。然后可以有第二个向量int(或size_t),其中time[i]最初是i,但随后根据另一个向量进行排序。因此,time[i]的flow是flow[time[i]],您可以根据该值进行排序(您需要一个函子)。然后,您可以使用类似的函子,使用二进制搜索计算出流量为一定量的时间

问题回答

您正在寻找的是一个双向映射,实现这一点的一种常见方法实际上是拥有两个独立的映射。或者,您也可以使用一些库实现,例如增强。双映射





相关问题
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?

热门标签