#include <iostream>
//#include <iomanip>
#include <cmath>
using namespace std;
template <size_t N>
class Vector { public:
float Components[N];
void Add(const Vector & right) {
for(size_t i = 0; i < N; ++i) {
Components[i] += right.Components[i];
}
}
void Subtract(const Vector & right) {
for(size_t i = 0; i < N; ++i) {
Components[i] -= right.Components[i];
}
}
float LengthSquared() const {
float result = 0.0f;
for(auto component : Components) {
result += component * component;
}
return result;
}
float Length() const {
return sqrt(LengthSquared());
}
};
template <size_t N>
ostream & operator<<(ostream & left, const Vector<N> & right) {
left<<"{";
for(auto component : right.Components) {
left<<component<<",";
}
left<<"}";
return left;
}
int main() {
// /O2 /Oi /MACHINE:X64
// MSC 1800
// call 命令が含まれる箇所が
// cout の部分だけにまで展開?された
Vector<3> v1 = {2.0f,3.0f,0.0f};
Vector<3> v2 = {1.0f,5.0f,1.0f}; v2.Subtract(v1);
Vector<3> v3 = v1; v3.Add(v2);
float l1 = v1.Length();
float l2 = v2.Length();
float l3 = v3.Length();
cout<<v1<<" "<<l1<<endl;
cout<<v2<<" "<<l2<<endl;
cout<<v3<<" "<<l3<<endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgovLyNpbmNsdWRlIDxpb21hbmlwPgojaW5jbHVkZSA8Y21hdGg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0ZW1wbGF0ZSA8c2l6ZV90IE4+CmNsYXNzIFZlY3RvciB7IHB1YmxpYzoKCWZsb2F0IENvbXBvbmVudHNbTl07Cgl2b2lkIEFkZChjb25zdCBWZWN0b3IgJiByaWdodCkgewoJCWZvcihzaXplX3QgaSA9IDA7IGkgPCBOOyArK2kpIHsKCQkJQ29tcG9uZW50c1tpXSArPSByaWdodC5Db21wb25lbnRzW2ldOwoJCX0KCX0KCXZvaWQgU3VidHJhY3QoY29uc3QgVmVjdG9yICYgcmlnaHQpIHsKCQlmb3Ioc2l6ZV90IGkgPSAwOyBpIDwgTjsgKytpKSB7CgkJCUNvbXBvbmVudHNbaV0gLT0gcmlnaHQuQ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