问题是如何使输出文件1在第1行,2第2行等,因为程序因为它在每次执行循环时重写文件而你只留9在输出文件中.
#include <fstream>
using namespace std;
void function (int i)
{
ofstream output("result.out");
output << i << endl;
output.close();
}
int main()
{
for (int i=1; i<10; i++)
{
function(i);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在C++11其中std::atomic_flag,对于线程循环很有用:
static std::atomic_flag s_done(ATOMIC_FLAG_INIT);
void ThreadMain() {
while (s_done.test_and_set()) { // returns current value of s_done and sets to true
// do some stuff in a thread
}
}
// Later:
s_done.clear(); // Sets s_done to false so the thread loop will drop out
Run Code Online (Sandbox Code Playgroud)
该ATOMIC_FLAG_INIT组的标志false,这意味着该线程永远不会在循环。一个(不好的)解决方案可能是这样做的:
void ThreadMain() {
// Sets the flag to true but erases a possible false
// which is bad as we may get into a deadlock
s_done.test_and_set();
while …Run Code Online (Sandbox Code Playgroud) std::string pattern = "[disk0-9]";
std::regex regex(pattern, std::regex::ECMAScript);
std::string subject = "Disk1";
bool result = std::regex_match(subject, regex, std::regex_constants::match_any);
std::cout << result << std::endl;
Run Code Online (Sandbox Code Playgroud)
regex_match请问为什么返回假?
假设我在一个文本文件中有一组1000个统计数据.其第一列表示索引的数量,第二列表示该值的值.索引可以重复,相应的值可以不同.我想计算索引的出现次数和每个索引的值的总和.
我编写了一个代码,它给出了索引出现的结果,但它没有给出相应的值总和.
例
假设我的文本文件有一组这样的数据 -
#index value
4 0.51
5 0.13
5 0.53
5 0.25
6 0.16
6 0.16
7 0.38
4 0.11
3 0.101
4 0.32
4 0.2 ... and more
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下 -
指数4 出现 4次,相应的值之和 =(0.51 + 0.11 + 0.32 + 0.2)= 1.14
同样
指数5 出现 2次,值之和 =(0.13 + 0.53)= 0.66等.
我的守则
这是我的代码 -
#include <iostream>
#include <map>
#include <fstream>
using namespace std;
int main()
{
map<double,double> index;
double number,value;
double total;
ifstream theFile ("a1.txt");
while(theFile …Run Code Online (Sandbox Code Playgroud) 让int * ptr,array[10]; ptr= array;.现在阵列的连续位置中的每个存储器单元具有固定的大小.如果第一个单元的地址是1234,那么下一个单元必须是1238地址.但我们使用指针作为访问它*(ptr+1).我很困惑.任何来源或答案?谢谢.
我的一项任务是运动考试,我有一点问题.这是一个文字:
类MyFloat有一个私有变量float num.您必须编写将启用下一行代码的方法:MyFloat x = 3.5; MyFloat y = x + 3.2 float z = 3.4 + y
我写这段代码:
#include <iostream>
#include <Windows.h>
using namespace std;
class MyFloat
{
float num;
public:
MyFloat(float n)
{
num = n;
}
MyFloat operator+(MyFloat x)
{
float result;
result = x.num + this->num;
return result;
}
};
int main()
{
MyFloat x = 3.75;
MyFloat y = x + 3.2;
float z = 3.4 + y;
system("PAUSE");
}
Run Code Online (Sandbox Code Playgroud)
我在这一行得到错误:
float z = 3.4 …Run Code Online (Sandbox Code Playgroud)