Last active
January 13, 2020 01:55
-
-
Save higaki/3587771b6ff05d5b16200588f1c1d624 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
ostream& operator<<(ostream& out, const pair<const string, int>& kv) | |
{ | |
out << kv.first << ":" << kv.second; | |
return out; | |
} | |
template <class T> | |
static void inspect_array(T i, T end) | |
{ | |
cout << "["; | |
for (auto sep = ""; i != end; ++i, sep = ", ") | |
cout << sep << *i; | |
cout << "]" << endl; | |
} | |
int main() | |
{ | |
int a[] = {1, 2, 3, 5}; | |
inspect_array(a, &a[sizeof(a)/sizeof(a[0])]); | |
cout << a[2] << endl; // => 3 | |
cout << a[4] << endl; // => 未定義? | |
for (auto i = 0; i < sizeof(a)/sizeof(a[0]); ++i) | |
cout << a[i] << endl; | |
for (auto p = a; p != &a[sizeof(a)/sizeof(a[0])]; ++p) | |
cout << *p << endl; | |
vector<int> v = {1, 2, 3, 5}; | |
inspect_array(v.begin(), v.end()); | |
cout << v[2] << endl; // => 3 | |
cout << v[4] << endl; // => 未定義? | |
for (auto i = v.begin(); i != v.end(); ++i) | |
cout << *i << endl; | |
map<string, int> h = { | |
{"matz", 54}, | |
{"dhh", 40}, | |
}; | |
inspect_array(h.begin(), h.end()); | |
cout << h["matz"] << endl; // => 54 | |
cout << h["linda"] << endl; // => 未定義? | |
for (auto kv = h.begin(); kv != h.end(); ++kv) | |
cout << kv->first << ": " << kv->second << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment