小编All*_*son的帖子

c ++中新分配的int的内存大小,是否有不同的更好的方式来查看它?

#include "stdafx.h"
#include <iostream>
#include <new>

int main(){

const int gib = 268435256; //Created a constant int so i could allocate 1 
                           //Gib memory
int *ptr = new int [gib];

std::cout << sizeof (int)*gib << std::endl;
std::cout << *ptr << std::endl;
std::cout << ptr << std::endl;

try {


}catch (std::bad_alloc e) {
    std::cerr << e.what() << std::endl;
}

system("PAUSE");
delete[] ptr;

return 0;


}
Run Code Online (Sandbox Code Playgroud)

在这个程序中,我试图找出为我的指针分配了多少内存,我可以通过这种方式看到它应该是1 gibibyte wich = 1 073 741 824字节.我的问题是,我可以得到这个的唯一方法是取int的大小为4并乘以该const数.有不同的方式吗?

c++ memory size allocation

2
推荐指数
1
解决办法
85
查看次数

是否可以更改地图中的一对?

所以我用地图和对测试一些东西,我遇到了问题.

    std::map<std::string, int> pairTest;

pairTest.insert(std::make_pair("Peter", 100));

for (std::map<std::string, int>::iterator it = pairTest.begin(); it != pairTest.end(); it++) {
    std::cout << it->first << ":" << it->second << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我怎么能把"彼得"变成丹尼尔?我本该能够做到吗?

我怎样才能使对永远不变,并添加不同价值的相同密钥?

我尝试过另一种方式,这是一种更正确的做法吗?

    std::map<std::string, int> pairTest;

pairTest.insert(std::pair<std::string, int>("Peter", 100));

for (std::map<std::string, int>::iterator it = pairTest.begin(); it != pairTest.end(); it++) {
    std::cout << it->first << ":" << it->second << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ stdmap

-1
推荐指数
1
解决办法
96
查看次数

标签 统计

c++ ×2

allocation ×1

memory ×1

size ×1

stdmap ×1