可能重复:
C++:"std :: endl"vs"\n"
我有一个简单的程序,我测试过,我意识到这endl对我的程序造成了严重破坏.使用ENDL,我的程序在运行100个+ MS与工作时'\n',时间降至〜50毫秒.有谁能说出为什么会有这样的差异?
PS我确实阅读了其他帖子,以某种方式解释了他们每个人在做什么,但std::flush确实花了这么多时间?
或者还有其他可能的解释吗?
我已经实现了一个isPermutation函数,给定两个字符串将返回,true如果这两个是彼此的排列,否则它将返回false.
一个使用c ++排序算法两次,而另一个使用一个int数组来跟踪字符串计数.
我运行代码几次,每次排序方法更快.我的阵列实现错了吗?
这是输出:
1
0
1
Time: 0.088 ms
1
0
1
Time: 0.014 ms
Run Code Online (Sandbox Code Playgroud)
和代码:
#include <iostream> // cout
#include <string> // string
#include <cstring> // memset
#include <algorithm> // sort
#include <ctime> // clock_t
using namespace std;
#define MAX_CHAR 255
void PrintTimeDiff(clock_t start, clock_t end) {
std::cout << "Time: " << (end - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl;
}
// using array to keep a …Run Code Online (Sandbox Code Playgroud) c++ optimization performance permutation performance-testing
我是编程的新手,我昨天开始自学,我已经得到了所有东西,但老实说,我不明白它们之间的区别
std::cout << x;
Run Code Online (Sandbox Code Playgroud)
和
std::cout << x << std::endl;
Run Code Online (Sandbox Code Playgroud)
没有人向我解释这一点,我要求保持安全.
我的任务很简单 - 我只需要解析这样的文件:
Apple = 1
Orange = 2
XYZ = 3950
Run Code Online (Sandbox Code Playgroud)
但我不知道可用密钥的集合.我使用C#解析这个文件比较容易,让我演示源代码:
public static Dictionary<string, string> ReadParametersFromFile(string path)
{
string[] linesDirty = File.ReadAllLines(path);
string[] lines = linesDirty.Where(
str => !String.IsNullOrWhiteSpace(str) && !str.StartsWith("//")).ToArray();
var dict = lines.Select(s => s.Split(new char[] { '=' }))
.ToDictionary(s => s[0].Trim(), s => s[1].Trim());
return dict;
}
Run Code Online (Sandbox Code Playgroud)
现在我只需要使用c ++做同样的事情.我想要使用,boost::property_tree::ptree但似乎我不能迭代ini文件.它很容易阅读ini文件:
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini(path, pt);
Run Code Online (Sandbox Code Playgroud)
但是不可能迭代它,参考这个问题Boost程序选项 - 获取部分中的所有条目
问题是 - 在C++上编写C#代码模拟的最简单方法是什么?
我有一个3x3 2D阵列.我想要达到它的所有元素.可能吗?我这样做:
int myArray[3][3];
for(int &i: myArray){
//MY CODE HERE.
}
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我得到错误:
error: C2440: 'initializing' : cannot convert from 'int [3]' to 'int &'
Run Code Online (Sandbox Code Playgroud)
我也在Qt 5.0 x64上使用MSVC++ 2012编译器.如果可以这样做,那么我如何获得每个元素的索引号?
我希望输出说出用户按下的箭头键.我得到的输出是:
"You pressed the Dz? button."
Run Code Online (Sandbox Code Playgroud)
我写的是:
unsigned int key;
string K;
do
{
key = getch();
if (key==72)
K=" up";
else if(key==80)
K="down";
else if (key==77)
K="right";
else if (key==75)
K="left";
else
{}
if (key!=224)
printf("You pressed the %s button.\n", K);
else
{}
}while(key !='q');
return;
Run Code Online (Sandbox Code Playgroud) 我在这里剪掉了我班上不相关的部分.我不知道我做错了什么,只是想尝试能够cout <<对象.
#include <iostream>
class Snipped
{
public:
friend std::ostream& operator<<(std::ostream& os, const Snipped& s);
protected:
private:
};
std::ostream& operator<<(std::ostream& os, const Snipped& s)
{
os << "test";
return os;
}
int main(int argc, char* argv[])
{
Snipped* s = new Snipped();
std::cout << s << std::endl << s;
delete s;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
test
test
Run Code Online (Sandbox Code Playgroud)
实际产量:
0x12ae20
0x12ae20 (random memory location?)
Run Code Online (Sandbox Code Playgroud) 我的程序打印了大量的短行cout.
作为一个有点人为的例子,我的线条看起来有点像这样:
cout<<"The variable's value is: "<<variable<<endl;
Run Code Online (Sandbox Code Playgroud)
我希望程序能够快速运行,我相信这会让我感到害怕,endl因为它会在cout每次使用时启动缓冲区刷新.
现在,互联网上的一些人说我可以这样做:
cout<<"The variable's value is: "<<variable<<"\n";
Run Code Online (Sandbox Code Playgroud)
但这似乎不是一个好的解决方案,因为endl抽象特定于系统的特定方式可以指定终止线,而\n不是.这似乎也是一个糟糕的解决方案,因为,如果我将来需要缓冲,那么我将不得不修改整个代码库.
因此,我问,有没有办法禁用缓冲区刷新方面endl?
编辑
进一步的挖掘似乎表明了两者endl并且\n尊重OS可能选择的各种方式来结束它的线条.似乎输出流检测它是否处于潜在的交互状态,并相应地缓冲和刷新.因此:问题可以通过手动告诉输出流执行积极缓冲来解决...如果我能弄清楚如何做到这一点.
我有一个程序,使用for循环打印出字符串的字符.它还必须反向打印相同的字符,这是我遇到问题的地方.有人可以帮我弄清楚为什么第二个for循环没有执行?
int main()
{
string myAnimal;
cout << "Please enter the name of your favorite animal.\n";
cin >> myAnimal;
// This loop works fine
int i;
for(i = 0; i < myAnimal.length(); i++){
cout << myAnimal.at(i) << endl;
}
// This one isn't executing
for(i = myAnimal.length(); i > -1; i--){
cout << myAnimal.at(i) << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×10
string ×2
boost ×1
endl ×1
for-loop ×1
foreach ×1
iostream ×1
newline ×1
optimization ×1
outputstream ×1
overloading ×1
performance ×1
permutation ×1
platform ×1
printf ×1
reverse ×1
stream ×1
visual-c++ ×1