Skip to content

Instantly share code, notes, and snippets.

@higaki
Last active January 13, 2020 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save higaki/3587771b6ff05d5b16200588f1c1d624 to your computer and use it in GitHub Desktop.
Save higaki/3587771b6ff05d5b16200588f1c1d624 to your computer and use it in GitHub Desktop.
#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