相关疑难解决方法(0)

带有std :: string的C++ printf?

我的理解是它stringstd命名空间的成员,为什么会出现以下情况呢?

#include <iostream>

int main()
{
    using namespace std;

    string myString = "Press ENTER to quit program!";
    cout << "Come up and C++ me some time." << endl;
    printf("Follow this command: %s", myString);
    cin.get();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

每次程序运行时,都会myString打印一个看似随机的3个字符的字符串,例如上面的输出.

c++ string printf namespaces std

144
推荐指数
4
解决办法
32万
查看次数

C和C++中几乎完全相同的代码之间的执行时间差异很大(x9)

我试图从www.spoj.com解决这个练习:FCTRL - Factorial

你真的不必阅读它,只要你好奇就去做:)

首先我用C++实现它(这是我的解决方案):

#include <iostream>
using namespace std;

int main() {
    unsigned int num_of_inputs;
    unsigned int fact_num;
    unsigned int num_of_trailing_zeros;

    std::ios_base::sync_with_stdio(false); // turn off synchronization with the C library’s stdio buffers (from https://stackoverflow.com/a/22225421/5218277)

    cin >> num_of_inputs;

    while (num_of_inputs--)
    {
        cin >> fact_num;

        num_of_trailing_zeros = 0;

        for (unsigned int fives = 5; fives <= fact_num; fives *= 5)
            num_of_trailing_zeros += fact_num/fives;

        cout << num_of_trailing_zeros << "\n";
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我上传它作为g ++ 5.1的解决方案

结果是: …

c c++ performance gcc iostream

85
推荐指数
3
解决办法
6168
查看次数

C++ iostream 与 C stdio 性能/开销

我试图理解如何提高这个 C++ 代码的性能,使其与它所基于的 C 代码相提并论。C 代码如下所示:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct point {
  double x, y;
} point_t;

int read_point(FILE *fp, point_t *p) {
  char buf[1024];
  if (fgets(buf, 1024, fp)) {
    char *s = strtok(buf, " ");
    if (s) p->x = atof(s); else return 0;
    s = strtok(buf, " ");
    if (s) p->y = atof(s); else return 0;
  }
  else
    return 0;
  return 1;
}

int main() {
  point_t p;
  FILE *fp = fopen("biginput.txt", "r");

  int i …
Run Code Online (Sandbox Code Playgroud)

c++ performance iostream

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

为什么snprintf在打印单个数字时始终比ostringstream快2倍?

我正在测试各种double在C ++ 中格式化s的方法,下面是一些代码:

#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <sstream>
#include <iostream>

inline long double currentTime()
{
    const auto now = std::chrono::steady_clock::now().time_since_epoch();
    return std::chrono::duration<long double>(now).count();
}

int main()
{
    std::mt19937 mt(std::random_device{}());
    std::normal_distribution<long double> dist(0, 1e280);
    static const auto rng=[&](){return dist(mt);};
    std::vector<double> numbers;
    for(int i=0;i<10000;++i)
        numbers.emplace_back(rng());

    const int precMax=200;
    const int precStep=10;

    char buf[10000];
    std::cout << "snprintf\n";
    for(int precision=10;precision<=precMax;precision+=precStep)
    {
        const auto t0=currentTime();
        for(const auto num : numbers)
            std::snprintf(buf, sizeof buf, "%.*e", precision, num);
        const auto …
Run Code Online (Sandbox Code Playgroud)

c++ performance output

2
推荐指数
1
解决办法
146
查看次数

哪个更快,哪个更灵活:printf或cout?

可能重复:
在C++ cin或printf中printf vs cout

我一直想知道printf和cout ..哪一个最终更快,并且它也是最灵活的(即可以打印一系列变量,输出可以格式化)?

PS我知道这看起来类似于C++中的'printf'和'cout',但我并不是真的在问同样的事情.

c++ printf cout

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

标签 统计

c++ ×5

performance ×3

iostream ×2

printf ×2

c ×1

cout ×1

gcc ×1

namespaces ×1

output ×1

std ×1

string ×1