Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将向量的内容写入文件c++;_C++_File Io - Fatal编程技术网

C++ 将向量的内容写入文件c++;

C++ 将向量的内容写入文件c++;,c++,file-io,C++,File Io,我试图将我的矢量内容写入一个文件。为此,我编写了一段代码,如: int main() { ofstream outputfile("test.txt"); vector<int>temp; temp.push_back(1); temp.push_back(2); temp.push_back(3); for(int i=0;i<temp.size();i++) outputfile<<temp[i]&

我试图将我的矢量内容写入一个文件。为此,我编写了一段代码,如:

int main()
{
    ofstream outputfile("test.txt");
    vector<int>temp;
    temp.push_back(1);
    temp.push_back(2);
    temp.push_back(3);
    for(int i=0;i<temp.size();i++)
        outputfile<<temp[i]<<"\n";
}
intmain()
{
流输出文件(“test.txt”);
维克托坦;
温度推回(1);
温度推回(2);
温度推回(3);
对于(int i=0;i您可以使用

std::copy(temp.rbegin(), temp.rend(),
          std::ostream_iterator<int>(outputfile, "\n"));
std::copy(temp.rbegin(),temp.rend(),
std::ostream_迭代器(outputfile,“\n”);
该代码:

for(int i=temp.size()-1;i>=0;i--)
    outputfile<<temp[i]<<"\n";
for(int i=temp.size()-1;i>=0;i--)

outputfile您可以在一行中完成所有操作:

std::copy(temp.rbegin(), temp.rend(), std::ostream_iterator<int>(outputFile, "\n"));
std::copy(temp.rbegin(),temp.rend(),std::ostream\u迭代器(outputFile,“\n”);

使用反向迭代器:

for (std::vector<int>::reverse_iterator it = myvector.rbegin(); it != myvector.rend(); ++it)
而不是

for(int i=temp.size();i>=0;i--)
复制(head\u buff.rbegin(),head\u buff.rend(), std::ostream_迭代器(数据包“\n”);
但是使用
#include#include#include
第二种方法是遍历整个向量并将内容复制到字符串中 然后将字符串写入ofstream 即

std::字符串somthing;
对于(std::vector::const_迭代器i=temp.begin();i!=temp.end();++i)
{
某物+=*i;
}
然后将字符串(某物)写入流ie

std::ofstram output;
output.open("test.txt",std::ios_base::trunc)
if(output.fail())
  std::cerr<<"unable to open the file"std::endl;
output << something;
//after writing close file
 output.close();
std::ofstram输出;
output.open(“test.txt”,std::ios\u base::trunc)
if(output.fail())

std::cerrYou从
向量的末尾开始算起
@BoBTFish抱歉,我纠正了这个错误。它在我的真实代码中的位置是正确的。所以,问题不是这样。如果你的代码实际上给了你一个空文件(一旦刷新和/或关闭),问题一定出在你没有展示的地方。到目前为止,你得到的所有答案都没有抓住问题的关键。代码对我来说也很有用,尽管我会将其更改为使用反向迭代器。谢谢你的回答。因为在我真正的问题中,我的向量大小将达到10000,效率很重要,复制过程会导致问题吗?@caesar并不比其他任何东西都重要。大部分时间可能是1)IO本身,然后是2)文本转换。其余时间,副本将与循环中的操作基本相同。我想你指的是反向迭代器,带有rbegin()和rend()人们真的觉得这比简单的for循环更容易理解吗?无论如何,它并没有真正解决为什么他的for循环不起作用的问题,因为它应该起作用。@BenjaminLindley一旦你习惯了,是的(至少我是这样做的)。因为您不需要查看并确保循环执行它似乎执行的操作。@BenjaminLindley是的。在尝试跟踪以奇怪方式设置条件的自定义循环中的错误之后(例如,从1开始执行<而不是@Angew:您仍然需要确保参数是正确的。对我来说,检查参数到如此简单的for循环所需的时间与检查参数所需的时间是无法区分的。但每个人都有自己的时间。
for(int i=temp.size();i>=0;i--)
std::copy(head_buff.rbegin(), head_buff.rend(),
          std::ostream_iterator<std::string>(data_pack, "\n"));
std::string somthing;
for(std::vector<std::string>::const_iterator i = temp.begin();i!=temp.end(); ++i)
 {
     something+=*i;
 }
std::ofstram output;
output.open("test.txt",std::ios_base::trunc)
if(output.fail())
  std::cerr<<"unable to open the file"std::endl;
output << something;
//after writing close file
 output.close();