c语言重组一个数,c++ - 使用C ++在文件中剪切和重组数字 - SO中文参考 - www.soinside.com...

因此,给出了答案。很好,正确。已接受并接受。很好。

仅作为附加信息,我将展示使用更现代的语言元素的C ++解决方案。该解决方案跳过了中间文件(可以由一个文件名生成),也不需要使用“ X”(也可以非常简单地添加)。

使用STL,我们可以用净的5行代码提供从原始源文件到最终目标文件的解决方案。

请参阅:#include

#include

#include

#include

#include

#include

#include

std::regex reFloat{ R"([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)" };

using SVector = std::vector<:string>;

using SVectorIter = SVector::iterator;

int main() {

// Open source file and check, if it coud be opened

if (std::ifstream sourceFileStream{ "r:\\dummyValues.dat" }; sourceFileStream) {

// Open destination file and check, if it could be opened

if (std::ofstream finalFileStream("r:\\benchmarks_final.dat"); finalFileStream) {

// Algorithm start ----------------------------------------------------------

// Define a string variable and initialize it with the contents of the file

std::string completeFile(std::istreambuf_iterator(sourceFileStream), {});

// Define vector and initialize it with all float values from the file

SVector values(std::sregex_token_iterator(completeFile.begin(), completeFile.end(), reFloat), {});

// Iterate over the vector and find the next value equal to first-value

for (SVectorIter svi{ values.begin() }; (svi = std::find(svi, values.end(), values[0])) != values.end(); ++svi) {

// Copy 16 value to the final file

std::copy_n(svi, std::min(16, std::distance(svi, values.end())), std::ostream_iterator<:string>(finalFileStream, " "));

finalFileStream << '\n';

}

// Algorithm end ----------------------------------------------------------

}

else {

std::cerr << "\n*** Error: Could not open final file\n";

}

}

else {

std::cerr << "\n*** Error: Could not open source file\n";

}

}

我想不是,但是如果您有兴趣了解这一点,那么我会向您解释。请问,我很乐意提供帮助-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值