- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Code :
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void printVector (const vector<string>& theVect, vector<bool>& bArray, int nItems){
for (int i = 0; i < nItems; ++i)
if (bArray[i] == true)
cout << theVect[i] << " ";
outFile << theVect[i];
cout << "\n";
outFile << "\n";
}
void nCombination(const vector<string> &Vect,int n, int r){
vector<bool> myBits(n, false); // everything is false now
int count = 1;
for (size_t i = n-1; i >= 0 && count <= r; --i, ++count){
myBits[i] = true;
}
do // start combination generator
{
printVector(Vect, myBits, n );
} while (next_permutation(myBits.begin(), myBits.end()));; // change the bit pattern
}
void nPermutation(vector<string> o, int r){
do {
for(int count = 0; count < r; count++){
cout << o[count] << " " ;
outFile << o[count] << " ";
}
cout<< endl;
outFile << endl;
}
while (next_permutation(o.begin(), o.end()));
}
int main(int argc, char** argv) {
int numofOps;
char Op;
int n;
int r;
string line;
ifstream myFile("infile.dat");
myFile >> numofOps;
myFile.ignore(1,'\n');
ofstream outFile;
outFile.open ("example.txt");
for(int q = 0; q < numofOps; ++q){
myFile.get(Op);
myFile >> n;
myFile>> r;
vector<string> original(n);
for(int i = 0;i <= n - 1; i++){
myFile.ignore(1,'\n');
myFile >> original[i];}
myFile.ignore(1,'\n');
sort(original.begin(), original.end());
cout<< '\n'<< endl;
if(Op == 'P'){
nPermutation(original, r);
}
else{
if(Op == 'C')
nCombination(original,n,r);
else
cout << "Incorrect Input" << endl;
}
original.clear();
}
outFile.close();
return 0;
}
我的代码可以工作,但是当我添加 outFile 以将所有内容写入名为 example 的文件时,它不起作用。如何打印出函数的结果??
例子会很好!!
最佳答案
想到一个直接的问题:
if (bArray[i] == true)
cout << theVect[i] << " ";
outFile << theVect[i];
Python 是我所知道的唯一一种使用缩进来控制语句 block 的语言。在基于 C 的语言中,您应该使用大括号:
if (bArray[i] == true) {
cout << theVect[i] << " ";
outFile << theVect[i];
}
最重要的是,您提供的代码甚至不应该编译。您在 main()
中将 outFile
创建为 local 参数,然后尝试在无法访问的其他函数中使用它。
您应该通过将其移出 main()
使其成为“全局”,以便每个函数都可以看到它(不明智,但可能是这里最快的解决方案)或将其作为参数传递给每个需要使用它的函数。
对于前者,您可以使用:
using namespace std;
ofstream outFile; // taken from main().
对于后者,这是在每次调用 printVector()
、nCombination()
和 nPermutation()
时添加它的问题,并修改这些功能,以便他们接受它,例如:
void nPermutation (
vector<string> o,
int r,
ofstream os
) {
os << "blah blah blah\n";
}
:
nPermutation (original, r, outFile);
关于c++ - Quick Q - 无法将函数的输出写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27308011/
当我打开一个 Xcode 项目时,快速帮助功能工作得很好,但是当打开一个不在打开的项目中的文件(例如 abc.swift )时,文档不会出现。 下面是一个例子: 最佳答案 安装版本Command Li
我扩展了org.eclipse.ui.editors.text.TextEditor来实现自定义编辑器。对于此编辑器,我定义了一个标记类型(org.eclipse.core.resources.mar
我将 Xcode 7 beta 5(这是当前安装的唯一 xcode)与 Swift 2 一起使用,当我尝试使用 Quick 运行单元测试时,出现以下错误: error: module file was
我有一个 Excel 文档,从另一个 xls 中提取项目作为列表项来填充下拉菜单。问题是,有很多选择。我找不到快速到达我想要的区域的方法。 (例如,打开下拉列表并按“b”不会像大多数普通下拉列表那样转
如果出现给定情况,我有一组测试会使整个测试套件崩溃。而且,那种情况对于套件来说已经是失败了。 与其崩溃,我宁愿在系列设置期间测试条件是否为真,并在失败时快速中止它。 这可能吗? 最佳答案 是的,您可以
Currentyl 我正在开发一个 Java Web 应用程序,一些简单的 JSF + Spring + Hibernate 教科书应用程序架构,但我发现每次我必须这样做时,我都会在将 WAR 上传到
当我使用 --quick option 运行 mysql 时正如另一个网络帖子中所建议的描述 返回很多 -- 而当我在没有 --quick 的情况下运行 mysql 时选项 desc table返回正
是解决Quick sort中最差时间复杂度的中位数3吗? 还有, 使用 3 的中位数方法时是否存在最坏情况的时间复杂度? 最佳答案 我认为一直以中位数为基准的快速排序的最差时间复杂度是 O(nlogn
我想创建一个自定义对话框,例如联系人应用程序中的“快速联系”或官方 Twitter 应用程序中的对话框。当我点击一个按钮时,它会在按钮上方或下方打开。 http://londatiga.net/ima
开始为 Linux 平台编写 C 代码,我正在使用 man 工具来获取有关标准库函数的信息。现在我想知道有什么解决方案可以轻松获取有关这些函数使用的结构的信息。 此处的示例是 man sched_se
1.视图模型介绍 在qml中、常见的view视图有: listview: 列表视图,视图中数据来自listmodel、xmllistmodel或c++中继承自qabstractitemmo
目录 1.Popup介绍 2.自定义Popup 1.Popup介绍 Popup是一个弹出窗口的控件 它的常用属性如下所示: ancho
PathView ,顾名思义,沿着特定的路径显示 Model 内的数据。 Model 能够是 QML 内建的 ListModel 、 XmlListModel ,也能够是在 C++ 中实现的 QAb
[对于那些不在家的人,这是 Rolling My Own Version Control 的续集;)] 所以我放弃并安装了 TortoiseSVN(为了和 friend 一起做一个项目,我的个人版本控
我试图在Qt Quick应用程序中打开一个链接,收到functions are not supported in Qt Quick ui form警告,该应用程序正常工作,我想摆脱该警告,如何解决此警
我对 Qt 和 Qt Quick 非常陌生。我正在验证 Qt Quick 的 Qt Test 单元测试框架,但我无法理解如何运行测试。这就是我所拥有的,我创建了一个具有以下结构的 SUBDIRS 项目
我有执行简单选择的存储过程。任何时候我手动运行它,它都会在秒下运行。但是在生产中(SQL Azure S2 数据库),它每 12 个我们的计划任务内运行一次 - 所以我认为期望它每次都以“冷”运行是合
我正在使用quick-Json在我的程序中序列化 HashMap(String, String[]) ,但我在反序列化该对象时遇到问题。我使用此代码来序列化 map JsonGenerator
是的,我知道这个问题在技术上将是“开放式的”,并且可能会“引起辩论”,但请记住,我并不是在这里询问方向,我只是想确保我在右场。所以,话虽这么说,这是我的问题: 我想创建一个程序,允许用户输入并存储 1
我想拦截Qt Quick事件,比如key events ,这样我就可以在它们到达当前目标之前处理它们,例如当前的 focused item ,可选择阻止事件在默认事件链中传播。可以通过QML代码处理事
我是一名优秀的程序员,十分优秀!