小编Jab*_*bba的帖子

混合cout和printf以获得更快的输出

在进行了一些测试后,我发现它printf比它快得多cout.我知道它依赖于实现,但在我的Linux机器上printf速度提高了8倍.所以我的想法是混合两种打印方法:我想cout用于简单的打印,我打算printf用于生成大量输出(通常在循环中).只要在切换到其他方法之前不忘记刷新,我认为这样做是安全的:

cout << "Hello" << endl;
cout.flush();

for (int i=0; i<1000000; ++i) {
    printf("World!\n");
}
fflush(stdout);

cout << "last line" << endl;
cout << flush;
Run Code Online (Sandbox Code Playgroud)

这样好吗?

更新:感谢所有宝贵的反馈.答案摘要:如果你想避免棘手的解决方案,只需简单地不使用endl,cout因为它会隐式刷新缓冲区.请"\n"改用.如果你产生大量输出会很有趣.

c++ performance printf cout

48
推荐指数
6
解决办法
2万
查看次数

头文件之间的循环依赖

我正在尝试用两个类实现一个树状结构:TreeNode.问题是,从每个类我想调用另一个类的函数,所以简单的前向声明是不够的.

我们来看一个例子:

tree.h中:

#ifndef TREE_20100118
#define TREE_20100118

#include <vector>
#include "Node.h"

class Tree
{
    int counter_;
    std::vector<Node> nodes_;

public:

    Tree() : counter_(0) {}

    void start() {
        for (int i=0; i<3; ++i) {
            Node node(this, i);
            this->nodes_.push_back(node);
        }
        nodes_[0].hi();    // calling a function of Node
    }

    void incCnt() {
        ++counter_;
    }

    void decCnt() {
        --counter_;
    }

};

#endif /* TREE_20100118 */
Run Code Online (Sandbox Code Playgroud)

Node.h:

#ifndef NODE_20100118
#define NODE_20100118

#include <iostream>
//#include "Tree.h"

class Tree;    // compile error without this …
Run Code Online (Sandbox Code Playgroud)

c++ dependencies header cyclic

9
推荐指数
1
解决办法
6052
查看次数

用户输入node.js

我有以下Node.js代码行为奇怪:

#!/usr/bin/env node

"use strict";

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function input(prompt) {
    rl.question(prompt, function (x) {
        rl.close();

        console.log("debug: " + x);
        return x;
    });
}

function main() {
    var n = input("Number: ");
//    console.log("value: " + n);    // problematic line
}

main();
Run Code Online (Sandbox Code Playgroud)

我想模仿Python raw_input,即从用户那里读取一行.显示提示后,应阻止程序,直到用户按Enter键.

如果"有问题的行"在注释中,它可以工作,程序正在等待输入.但是,如果此行不在注释中,则程序不会等待输入并n变为undefined.为什么?如何编写一个返回用户输入的函数?

input raw-input node.js

5
推荐指数
1
解决办法
5992
查看次数

将字符串转换为int

我有一个大文件,其中每行包含空格分隔的整数.任务是逐行稀疏此文件.对于字符串到int的转换,我有三个解决方案:

static int stringToIntV1(const string& str) {
    return (atoi(str.c_str()));
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我传递格式错误的字符串,则不会产生任何错误.例如,字符串"123error"被转换为123.

二解决方案:

static int stringToIntV2(const string& str)
{
    int result;
    istringstream myStream(str);

    if (myStream >> result) {
        return result;
    }
    // else
    throw domain_error(str + " is not an int!");
}
Run Code Online (Sandbox Code Playgroud)

我在这里遇到同样的问题,格式错误的字符串不会引发错误.

Boost的第三个解决方案(在Boost Library中找到):

static int stringToIntV3(const string& str)
{
    int iResult = 0;
    try {
        iResult = lexical_cast<int>(str);
    }
    catch(bad_lexical_cast &) {
        throw domain_error(str + " is not an int!");
    }
    return iResult;
}
Run Code Online (Sandbox Code Playgroud)

这个给出了正确的结果.

但是,执行时间存在显着差异.在大文本文件(32 MB)上测试,我得到以下时间: …

c++ string performance

4
推荐指数
1
解决办法
2843
查看次数

从函数发回一个向量

如何将以下Java代码正确地转换为C++?

Vector v;
v = getLargeVector();
...
Vector getLargeVector() {
    Vector v2 = new Vector();
    // fill v2
    return v2;
}
Run Code Online (Sandbox Code Playgroud)

所以这v是一个参考.该函数创建一个新的Vector对象并返回对它的引用.干净整洁.

但是,让我们看看以下C++镜像转换:

vector<int> v;
v = getLargeVector();
...
vector<int> getLargeVector() {
    vector<int> v2;
    // fill v2
    return v2;
}
Run Code Online (Sandbox Code Playgroud)

现在v是一个矢量对象,如果我理解正确,v = getLargeVector()复制函数返回的向量中的所有元素v,这可能很昂贵.此外,v2在堆栈上创建并返回它将导致另一个副本(但我知道现代编译器可以优化它).

目前我正是这样做的:

vector<int> v;
getLargeVector(v);
...
void getLargeVector(vector<int>& vec) {
    // fill vec
}
Run Code Online (Sandbox Code Playgroud)

但我觉得这不是一个优雅的解决方案.

所以我的问题是:做到这一点的最佳做法是什么(通过避免不必要的复制操作)?如果可能的话,我想避免正常的指针.到目前为止,我从未使用智能指针,我不知道他们是否可以在这里提供帮助.

c++ reference vector

3
推荐指数
1
解决办法
3893
查看次数