map<int,int>
を作成するとクリアできますが、それでもメモリに残りますか?例えば。C++でマップを破棄してマップポインタを設定するにはどうすればよいですか?
#include <map>
using namespace std;
int main(){
map<int,int> myMap;
myMap[1] = 2;
myMap.clear();
return 0;
}
しかし、私たちが代わりに実際のmap
のポインタを設定している場合、私はdelete
でそれを破壊できましたが、私はマップを同じように取り込むことができませんでした:
myMap[1] = 2;
ライン端をコメント解除
#include <map>
using namespace std;
int main(){
map<int,int> *myMap = new map<int,int>;
// myMap[1] = 2;
delete myMap;
return 0;
}
エラー:
[email protected]:~$ g++ test.cpp test.cpp: In function ‘int main()’: test.cpp:8:14: error: no match for ‘operator=’ (operand types are ‘std::map’ and ‘int’) myMap[1] = 2; ^ In file included from /usr/include/c++/5/map:61:0, from test.cpp:2: /usr/include/c++/5/bits/stl_map.h:296:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = int; _Tp = int; _Compare = std::less; _Alloc = std::allocator >] operator=(const map& __x) ^ /usr/include/c++/5/bits/stl_map.h:296:7: note: no known conversion for argument 1 from ‘int’ to ‘const std::map&’
C++でmap
をどのように破壊するのですか?それは "破壊的"なのだろうか?
また、map<>
ポインタの値を初期化/設定するにはどうすればよいですか?
'(* MYMAP)[1] = 2;あなたが 'new'を使うと、そのトリックを行います。残りの部分はCinCoutにあります。 – GMichael
同じ理由 'int * i = new int(0); i + = 5; delete i; 'はintに5を加えません。 – immibis
* "マップ'を作成した場合、それをクリアすることはできますが、それでもメモリに残っていますよね? "*、マップ管理オブジェクト自体は(おそらく1〜2ダースです32ビットまたは64ビットのアプリケーションをコンパイルし、 'map'実装がデバッグをサポートするためにデータを追加したかどうかを確認します)、アプリケーションによって再利用するために保存された' map'データ要素のメモリは解放されます。概して、マップ上で「クリア」以上のことをする価値はほとんどありません。これは、格納されていた要素が使用されていたメモリにハングアップしがちな「ベクトル」と対照的です。 –