C++ 将迭代器转换为字符串而不是cout?

C++ 将迭代器转换为字符串而不是cout?,c++,boost,copy,cout,C++,Boost,Copy,Cout,有人刚刚帮我用boost从目录中获取了一个文件名 if (exists(p)) // does p actually exist? { if (is_directory(p)) // is p a directory? { copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type

有人刚刚帮我用boost从目录中获取了一个文件名

    if (exists(p))    // does p actually exist?
    {

        if (is_directory(p))      // is p a directory?
        {
            copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type
                ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is

        }
    }
if(exists(p))//p实际上存在吗?
{
if(is_directory(p))//p是目录吗?
{
复制(目录迭代器(p),目录迭代器(),//目录迭代器::值迭代器类型
ostream_迭代器(cout,“\n”);//是目录项,它是
}
}
但是我想要一根绳子,而不是一根绳子


如何捕获该副本?

您可以将内容复制到
std::ostringstream
并使用
str()
检索缓冲区的副本:

std::ostringstream buf;
复制(目录迭代器(p),目录迭代器(),
std::ostream_迭代器(buf,“\n”);
字符串内容(buf.str());

您可以将内容复制到
std::ostringstream
中,并使用
str()
检索缓冲区的副本:

std::ostringstream buf;
复制(目录迭代器(p),目录迭代器(),
std::ostream_迭代器(buf,“\n”);
字符串内容(buf.str());

std::stringstream也会这样做。std::stringstream也会这样做。
std::ostringstream buf;

std::copy(directory_iterator(p), directory_iterator(),
          std::ostream_iterator<directory_entry>(buf, "\n"));

std::string content(buf.str());