- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Perl 的 IxHash 是一个关联数组,它会记住元素添加到其中的顺序,允许您通过键或索引直接 (O(1)) 访问它们。它还具有返回所有键或值的方法,始终按照它们最初插入的顺序。
C++ 是否有等效的容器类?
最佳答案
考虑 deque(或 array)和 unordered_map 的组合。如果你需要删除,它会是 O(n)(就像 ixhash)。迭代它们的键是一个肮脏的黑客。如果您想要一个真正的键和值迭代器,请查看 boost iterator_facade。
#include <unordered_map>
#include <deque>
#include <iterator>
#include <iostream>
using namespace std;
template <typename KeyType, typename ValueType>
class MapWithInsertionOrder {
public:
bool exists(const KeyType &kk)
{
return cache_.find(kk) != cache_.end();
}
bool store(const KeyType &kk, const ValueType &vv)
{
if (exists(kk))
{
return false;
}
keys_.push_back(kk);
cache_.insert(make_pair(kk, vv));
}
typedef unordered_map<KeyType,ValueType> cache_type;
typedef typename cache_type::value_type value_type;
typedef deque<KeyType> order_type;
typedef typename order_type::iterator order_iterator;
value_type &fetch(const KeyType &kk)
{
return *cache_.find(kk);
}
value_type &at(size_t idx)
{
auto kk = keys_.at(idx);
return fetch(kk);
}
size_t size()
{
return keys_.size();
}
order_iterator keys_begin()
{
return keys_.begin();
}
order_iterator keys_end()
{
return keys_.end();
}
private:
order_type keys_;
cache_type cache_;
};
int main()
{
MapWithInsertionOrder<string, string> mm;
mm.store( "cat", "tom" );
mm.store( "mouse", "jerry" );
mm.store( "bird", "tweety" );
mm.store( "dog", "spike" );
cout << mm.at(0).second << endl;
cout << mm.at(1).second << endl;
cout << mm.at(2).second << endl;
cout << mm.exists("cat") << endl;
cout << mm.exists("mouse") << endl;
cout << mm.exists("dog") << endl;
for( auto itr = mm.keys_begin();itr != mm.keys_end();itr++ )
{
cout << *itr << endl;
}
return 0;
}
关于c++ - Perl 的 Tie::IxHash(索引关联数组)的 C++ 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17495466/
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
用作 mergetool for Git 时,vimdiff 中与 kdiff3 的“从 A/B/C 中选择行”等效的是什么? kdiff3 中是否有类似 Ctrl+1/2/3 的快捷方式? 最佳答案
什么是 Javascript 等同于 imgsrc = $("#content div form img").attr('src'); HTML 是
您好,我有一个数据库可以从中选择 IP 位置> 脚本是在 php 中,我正在将它转换为 java,但我不知道什么是 ip2long('127.0.0.1' )); 在 java 中的等价物 最佳答案
我有一个 C# 应用程序,我正试图将其转换为 Java。 C# 应用程序有几个类型为 ushort 的变量。 Java 中是否有等效项? 谢谢 最佳答案 在大小方面最接近的等价物是 char,因为 J
我正在 iOS 中寻找与 .NET 中的脉冲和等待模式相同的多线程模式。本质上,我希望后台线程处于休眠状态,直到设置标志为止,这实际上是将线程“踢”到行动中。 它是 loop+thread.sle
对于某些并发编程,我可以使用 Java 的 CountDownLatch概念。是否有 C++11 的等效项,或者该概念在 C++ 中称为什么? 我想要的是在计数达到零时调用一个函数。 如果还没有,我会
我正在用 Ruby 开发一个 CLI 应用程序,我想允许通过 /etc/appnamerc 的标准配置文件级联在 Unix 中进行配置。 , ~/.appnamerc .但是,该应用程序也意味着在 W
是否有与 JAXB 等效的 PHP?它被证明对 Java 开发非常有用,作为一个 PHP 新手,我想在 PHP 世界中使用 JAXB 提供的相同概念。 最佳答案 我之前也想找同样的东西,但是找不到。所
Python 有一个 urljoin 函数,它接受两个 URL 并智能地连接它们。有没有在c++中提供类似功能的库? urljoin 文档:http://docs.python.org/library
我有一个从另一种语言移植的功能,你能帮我把它变成“pythonic”吗? 这里的函数以“非pythonic”方式移植(这是一个有点人为的例子 - 每个任务都与一个项目相关联或“无”,我们需要一个不同项
我有 2 个相同类型的对象,我想将一种状态浅复制到另一种状态。在 C++ 中,我有很棒的 memcpy。我怎样才能在 C# 中做到这一点? MemberwiseClone() 不够好,因为它创建并返回
有什么方法可以在 CSS 中使用条件语句吗? 最佳答案 我想说 CSS 中最接近“IF”的是媒体查询,例如可用于响应式设计的媒体查询。对于媒体查询,您是在说“如果屏幕宽度在 440 像素到 660 像
我正在尝试在 Swift 的 iTunesU 中从“为 iphone 和 ipad 开发 ios7 应用程序”中复制 Stanford Matchismo 游戏。 第三讲77页slides ,它显示使
这个问题在这里已经有了答案: Store output of subprocess.Popen call in a string [duplicate] (15 个回答) 关闭4年前。 我想从 pyt
这个问题在这里已经有了答案: Is there a 'foreach' function in Python 3? (14 个回答) 关闭1年前。 我正在深入研究 Python,但我有一个关于 for
我想从 Java 中的这个 Kotlin 类访问信息。它是通过 Gradle 库导入的。 密封类: public sealed class Resource private constructor()
SWT 中的 JPanel 有什么等价物? 最佳答案 原始问题要求 SWT 等同于 JLabel。 还有一个 org.eclipse.swt.custom.CLabel . SWT 等价于 JPane
在诸如 postgres 之类的 SQL 数据库中,我们可以创建 SCHEMA,以便我们可以将我们的表作为 schema_name.table_name 引用。 mongodb 中有模式吗?谢谢 最佳
哪个模型是“GBTRegressor”Pyspark 模型的 Python 等效模型? 简要背景:我正在尝试将 pyspark 模型重新创建为 python 模型。现有管道中使用的模型是 GBTReg
我是一名优秀的程序员,十分优秀!