:
std::vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
...しかし、私は通常、複数の行でそれをフォーマットします
01。
#include <iostream>
#include <vector>
using namespace std;
template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
const char *sep = "{ ";
for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
return out << " }";
}
int main()
{
vector<double> v1{ 1.0, 2.0, 3.0 }, v2{ 4.0, 5.0, 6.0 };
// in one line:
vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
// output:
cout << v3 << endl;
// done
return 0;
}
出力:
{ 4, 5, 6, 8, 10, 12, 12, 15, 18 }
(ideone上でテストされています。)
おかげで、ロリーDaulton、インスピレーションのためのMCVEとして
。 Cの++では、私もこれを行うことができます。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string operator*(const string &str1, const string &str2)
{
return str1 + str2;
}
template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
const char *sep = "{ ";
for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
return out << " }";
}
int main()
{
vector<string> v1{ "a", "b", "c" }, v2{ "d", "e", "f" };
// in one line:
vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
// output:
cout << v3 << endl;
// done
return 0;
}
出力:
{ ad, ae, af, bd, be, bf, cd, ce, cf }
を少し改善した後、それも混合型のために動作します:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string operator*(const string &arg1, int arg2)
{
string ret; for (int i = 0; i < arg2; ++i) ret += arg1;
return ret;
}
template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
const char *sep = "{ ";
for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
return out << " }";
}
int main()
{
vector<string> v1{ "a", "b", "c" }; vector<int> v2{ 1, 2, 3 };
// in one line:
vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
// output:
cout << v3 << endl;
// done
return 0;
}
出力:
{ a, aa, aaa, b, bb, bbb, c, cc, ccc }
まず特定の言語を選んでください。 –
'ad'と他の2つの実数の乗算、2つの文字列の連結、順序付けされたペアへの結合などがありますか? –
'V3'は実際には' V1'と 'V2'の行列積です(行列が行単位で格納されている場合)、要素が' int'、 'float'、または' double'と仮定すると、 [** Eigen library **](http://eigen.tuxfamily.org/index.php?title=Main_Page)を見てください。 Intels x86アーキテクチャでは、パフォーマンスを得るために[SSE命令](https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions)を使用します(少なくとも使用できます)。これは** C++ **で実装されており、** R **と** Python **の中でさまざまな言語のバインディングを持っています。 – Scheff