私はこの質問への最高の定格答えを文字列分割機能を使用したい:なぜ "void"の代わりに "std :: vector <std::string>"と表示されますか?
便宜のために答えをコピー:私はここに疑問を持っている
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
。なぜ宣言はvoid split(...)
ではなくstd::vector<std::string> &split(...)
ですか?議論として、私たちはstd::vector<std::string> &elems
を受け取り、これは私たちが終わらせたいものです。参照を受け取ると何かを返すのはなぜですか?
int &multA (int &n)
{
n = 5*n;
return n;
}
void multB (int &n)
{
n = 5*n;
}
と::
私が持っている
int n1, n2;
n1 = 1;
n2 = 1;
cout << "n1: " << n1 << endl;
cout << "n2: " << n2 << endl;
n1 = multA(n1);
multB(n2);
cout << "n1: " << n1 << endl;
cout << "n2: " << n2 << endl;
をし、その結果として、私が持っている:
n1: 1
n2: 1
n1: 5
n2: 5
違いは、その後何ですか?
これは単なる便利なので、必要に応じて戻り値を左辺値として使用できます。 – Aenimated1
これはメモリ管理と関係があります –