STL Maps -- Associative Arrays

本文介绍如何在 C++ 中使用 STL Map 类来存储关联数据。通过实例演示了如何声明、添加元素到 map,以及如何查找、删除和遍历 map 中的数据。此外还讨论了 map 的性能特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Suppose that you're working with some data that has values associated with strings -- for instance, you might have student usernames and you want to assign them grades. How would you go about storing this in C++? One option would be to write your own hash table. This will require writing a hash function and handling collisions, and lots of testing to make sure you got it right. On the other hand, the standard template library (STL) includes a templated class to handle just this sort of situation: the STL map class, which conceptually you can think of as an "associative array" -- key names are associated with particular values (e.g., you might use a student name as a key, and the student's grade as the data). 




In fact, the STL's map class allows you to store data by any type of key instead of simply by a numerical key, the way you must access an array or vector. So instead of having to compute a hash function and then access an array, you can just let the map class do it for you. 

To use the map class, you will need to include <map> and maps are part of the std  namespace. Maps require two, and possibly three, types for the template:
std::map <key_type, data_type, [comparison_function]>
Notice that the comparison function is in brackets, indicating that it is optional so long as your key_type has the less-than operator, <, defined -- so you don't need to specify a comparison function for so-called primitive types such as int, char, or even for the string class. Moreover, if you overloaded the < operator for your own class, this won't be necessary. 

The reason that the key type needs the less-than operator is that the keys will be stored in sorted order -- this means that if you want to retrieve every key, value pair stored in the map, you can retrieve them in the order of the keys. 

Let's go back to the example of storing student grades. Here's how you could declare a variable called grade_list that associates strings (student names) with characters (grades -- no + or - allowed!).
std::map <string, char> grade_list;
Now, to actually store or access data in the map, all you need to do is treat it like a normal array and use brackets. The only difference is that you no longer use integers for the index -- you use whatever data type was used for the key. 

For instance, following the previous example, if we wanted to assign a grade of 'B' to a student named "John", we could do the following:
grade_list["John"] = 'B';
If we later decided that John's grades had improved, we could change his grade in the same fashion:
grade_list["John"] = 'B';
// John's grade improves
grade_list["John"] = 'A';
So adding keys to a map can be done without doing anything special -- we just need to use the key and it will be added automatically along with the corresponding data item. On the other hand, getting rid of an element requires calling the function erase, which is a member of the map class:
erase(key_type key_value);
For instance, to erase "John" from our map, we could do the following:
grade_list.erase("John");
If we're curious, we could also check to see how many values the map contains by using the size function, which is also a member of the map class and other containers:
int size();
For instance, to find the size of our hypothetical class, we could call the size function on the grade list:
std::cout<<"The class is size "<<grade_list.size()<<std::endl;
If we're only interested in whether the map is empty, we can just use the map member function empty:
bool empty();
empty returns true if the map is empty; it returns false otherwise. 

If you want guarantee that the map is empty, you can use the clear function. For instance:
grade_list.clear();
would remove all students from the grade_list. 

What if you just wanted to check to see whether a particular key was in the map to begin with (e.g., if you wanted to know whether a student was in the class). You might think you could do something like this using the bracket notation, but then what will that return if the specified key has no associated value? 

Instead, you need to use the find function, which will return an  iterator pointing to the value of the element of the map associated with the particular key, or if the key isn't found, a pointer to the iterator  map_name.end()! The following code demonstrates the general method for checking whether a key has an associated value in a map.
std::map <string, char> grade_list;
grade_list["John"] = 'A';
if(grade_list.find("Tim") == grade_list.end())
{
    std::cout<<"Tim is not in the map!"<<endl;
}
Iterators can also be used as a general means for accessing the data stored in a map; you can use the basic technique from before of getting an iterator:
std::map<parameters>::iterator iterator_name;
where  parameters are the parameters used for the container class this iterator will be used for. This iterator can be used to iterate in sequence over the keys in the container. Ah, you ask, but how do I know the key stored in the container? Or, even better, what exactly does it mean to dereference an iterator over the map container? The answer is that when you dereference an iterator over the map class, you get a "pair", which essentially has two members, first and second. First corresponds to the key, second to the value. Note that because an iterator is treated like a pointer, to access the member variables, you need to use the arrow operator, ->, to "dereference" the iterator. 

For instance, the following sample shows the use of an iterator (pointing to the beginning of a map) to access the key and value.
std::map <string, char> grade_list;
grade_list["John"] = 'A';
// Should be John
std::cout<<grade_list.begin()->first<<endl;
// Should be A
std::cout<<grade_list.begin()->second<<endl;
Finally, you might wonder what the cost of using a map is. One issue to keep in mind is that insertion of a new key (and associated value) in a map, or lookup of the data associated with a key in a map, can take up to O(log(n)) time, where n is the size of the current map. This is potentially a bit slower than some hash tables with a good hashing function, and is due to the fact that the map keys are stored in sorted order for use by iterators. 

Summary 

The Good
  • Maps provide a way of using "associative arrays" that allow you to store data indexed by keys of any type you desire, instead of just integers as with arrays.
  • Maps can be accessed using iterators that are essentially pointers to templated objects of base type pair, which has two members, first and second. First corresponds to the key, second to the value associated with the key.
  • Maps are fast, guaranteeing O(log(n)) insertion and lookup time.
The Gotchas
  • Checking for whether an element is in a map requires using the find function and comparing the returned iterator with the iterator associated with the end of the map.
  • Since maps associate a key with only one value, you need to use a multimap (or make the type of the data a container) if you must associate multiple pieces of data with one key.
STL分解-LSTM是一种深度学习模型,它结合了STL(Seasonal and Trend decomposition using Loess)分解和LSTM(Long Short-Term Memory)模型的特点。在这种方法中,首先使用STL分解将时间序列数据分解为趋势、季节性和残差三个部分。然后,将分解后的趋势和季节性数据作为LSTM模型的输入,用于预测未来的时间序列值。通过结合STL分解和LSTM模型,STL分解-LSTM模型可以更好地捕捉时间序列数据的长期依赖和季节性变化,从而提高预测准确性。引用中的图7展示了ISTL-LSTM模型和STL-LSTM模型的预测结果的比较,而引用中的图6展示了不同深度学习模型(包括ISTL-LSTM,STL-LSTM,LSTM和GRU)在不同超参数设置下的预测效果比较。另外,引用给出了STL分解-LSTM模型的超参数设置,其中包括隐藏层数、神经元数量、训练迭代次数、batch size、dropout率、激活函数、损失函数和优化器的选择。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [《一种改进的STL-LSTM模型:用于COVID-19疫情期间每日公交客流预测》](https://blog.youkuaiyun.com/zuiyishihefang/article/details/123320971)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值