Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;,地图<&燃气轮机;结构,但有缺口 我试图用一个类来制作一个字典,在C++中,关键字,值> > < /P>_C++_Dictionary - Fatal编程技术网

C++;,地图<&燃气轮机;结构,但有缺口 我试图用一个类来制作一个字典,在C++中,关键字,值> > < /P>

C++;,地图<&燃气轮机;结构,但有缺口 我试图用一个类来制作一个字典,在C++中,关键字,值> > < /P>,c++,dictionary,C++,Dictionary,我在网上发现地图是我被支持使用的课程 但是,当我尝试使用Map时,它会填补关键点之间的空白 这是一个问题,因为键是数字,但它们非常稀疏 所以在一套中我可能有钥匙[1,20,30000,7000000]。我希望我的地图只存储这4个值,而不是1到7000000之间的每个值 #include <iostream> #include <map> using namespace std; int main() { map<int,int> a = {{1,-

我在网上发现地图是我被支持使用的课程

但是,当我尝试使用Map时,它会填补关键点之间的空白

这是一个问题,因为键是数字,但它们非常稀疏

所以在一套中我可能有钥匙[1,20,30000,7000000]。我希望我的地图只存储这4个值,而不是1到7000000之间的每个值

#include <iostream>
#include <map>

using namespace std;

int main()
{
    map<int,int> a = {{1,-1},{20,200}}; // I want this to just store {1,20}
    for(int x = 0; x < a.size(); x++) cout << a[p(x)] << ","; //however, when I print it I get [0,1..,19,20]
    return 0;
}

是否有一些解决方法来避免C++中“填充空缺”或STD中的其他类可用于此目的?

<代码> MAP::运算符[]/COD>为您创建条目(并增加它的代码> siz())/>代码>。如果您只想遍历
std::map
,请使用它的迭代器



for(auto&entry:a)cout正如苹果评论的那样,操作员[]创建了条目。见:

如果k与容器中任何元素的键不匹配,则函数将插入一个具有该键的新元素,并返回对其映射值的引用


如果要检查键是否存在,请使用
map::find()

以下是打印映射的一些方法:可以使用for-each循环或迭代器:

#include <iostream>
#include <map>
#include <random>

using namespace std;

int main()
{
    map<int, int> myMap;

    // filling map with random elements
    random_device rd;
    mt19937 rng(rd());
    uniform_int_distribution<int> uni(0,1000);
    for(int i = 0; i < 10; i++) {
        // uses the [] operator to create an element
        myMap[uni(rng)] = uni(rng);
    }

    // first method to print out map using for-each loop
    for(auto a : myMap) {
        // cannot change elements in map
        cout << a.first << " " << a.second << endl;
    }

    // second method to print out map using iterator
    for(map<int, int>::iterator it = myMap.begin(); it != myMap.end(); it++) {
        // can change elements in map
        cout << it->first << " " << it->second << endl;
    }
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
地图我的地图;
//用随机元素填充地图
随机器件rd;
mt19937 rng(rd());
统一国际分配单位(01000);
对于(int i=0;i<10;i++){
//使用[]运算符创建元素
myMap[uni(rng)]=uni(rng);
}
//使用for each循环打印地图的第一种方法
用于(自动a:myMap){
//无法更改映射中的元素

cout
map::operator[]
为您创建条目。谢谢苹果,但这不是我要问的。我正在寻找一个类,它可以像字典一样工作,而不会感觉到使用
a[p(x)]
中的
const map a
会有编译错误。苹果说的是当您使用[p(x)]要转储内容,它会创建一个条目。因此,苹果的解决方案是不使用[]操作符打印内容,这并不能填补空白
mapped_type& operator[] (const key_type& k);
#include <iostream>
#include <map>
#include <random>

using namespace std;

int main()
{
    map<int, int> myMap;

    // filling map with random elements
    random_device rd;
    mt19937 rng(rd());
    uniform_int_distribution<int> uni(0,1000);
    for(int i = 0; i < 10; i++) {
        // uses the [] operator to create an element
        myMap[uni(rng)] = uni(rng);
    }

    // first method to print out map using for-each loop
    for(auto a : myMap) {
        // cannot change elements in map
        cout << a.first << " " << a.second << endl;
    }

    // second method to print out map using iterator
    for(map<int, int>::iterator it = myMap.begin(); it != myMap.end(); it++) {
        // can change elements in map
        cout << it->first << " " << it->second << endl;
    }
}