또는 ostringstream?istringstream을 효율적으로 복사하는 방법은 무엇입니까?
istringstream a("asd");
istringstream b = a; // This does not work.
memcpy는 작동하지 않습니다.
또는 ostringstream?istringstream을 효율적으로 복사하는 방법은 무엇입니까?
istringstream a("asd");
istringstream b = a; // This does not work.
memcpy는 작동하지 않습니다.
istringstream a("asd");
istringstream b(a.str());
편집 : 당신은 또한 strinstream 내로 fstream의 전체 내용을 복사 할 수 있습니다처럼 다른 답변에 귀하의 의견을 바탕으로, 소리가 난다. 당신은 한 번에 하나의 성격을 원하지 않거나 할 필요가 없습니다 (그리고 당신 말이 맞습니다 - 보통은 꽤 느립니다).
// create fstream to read from
std::ifstream input("whatever");
// create stringstream to read the data into
std::istringstream buffer;
// read the whole fstream into the stringstream:
buffer << input.rdbuf();
+1 버퍼에 과부하가 있다는 것을 알지 못했습니다! – AraK
그래, 거의 아무도 모르는 그 귀여운 트릭 중 하나야. 그 모든 것을 자주 사용하지는 않지만, 원하는 것을 원할 때 좀 더 단순하게 만듭니다. –
'istringstream'대신 'ostringstream'을 사용하면 안됩니까? –
스트림을 복사 할 수는 없으며 반복기를 사용하여 버퍼를 복사해야합니다. 예를 들면 :
#include <sstream>
#include <algorithm>
......
std::stringstream first, second;
.....
std::istreambuf_iterator<char> begf(first), endf;
std::ostreambuf_iterator<char> begs(second);
std::copy(begf, endf, begs);
한 번에 한 문자를 복사하는 것이 느리지 않습니까? –
측정하셨습니까? – AraK
입력이 ifstringstream 인 파일 일 때 측정했습니다. 그 차이는 엄청났습니다. –
왜이 질문을 닫으시겠습니까? –
나도 궁금해? – GManNickG
나는 그것이 너무 유효한 질문이라고 생각한다. – AraK