相关疑难解决方法(0)

像sprintf一样的std :: string格式

我必须格式化std::stringsprintf,并将其发送到文件流.我怎样才能做到这一点?

c++ string formatting stl

412
推荐指数
17
解决办法
77万
查看次数

std :: string :: c_str()和temporaries

以下C++代码是否格式良好:

void consumer(char const* p)
{
  std::printf("%s", p);
}

std::string random_string_generator()
{
  // returns a random std::string object
}

consumer(random_string_generator().c_str());
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,在创建临时std :: string对象并获取c_str()指针后,没有什么能阻止std :: string对象被破坏(或者我错了?).如果代码一切正常,你能指点我的标准吗?当我使用g ++进行测试时,它确实有效.

c++ stl stdstring

56
推荐指数
3
解决办法
7924
查看次数

printf比std :: cout快5倍多?

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>

int main(int argc, char* argv[])
{
    std::clock_t start;
    double duration;    

    std::cout << "Starting std::cout test." << std::endl;
    start = std::clock();

    for (int i = 0; i < 1000; i++)
    {
        std::cout << "Hello, World! (" << i << ")" << std::endl;
    }

    duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;

    std::cout << "Ending std::cout test." << std::endl;
    std::cout << "Time taken: " << duration << std::endl;

    std::system("pause");

    std::cout << "Starting std::printf test." …
Run Code Online (Sandbox Code Playgroud)

c++ performance printf cout

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

printf()令人惊讶的控制台输出

经过几年的休整,我需要用C++开发.无论如何,我在阅读我正在阅读的教程后遇到了问题.在编写下面的代码片段后,我希望在我的控制台中看到"Hello World",但我只能看到'Debug:StrangeChars'; 什么地方出了错?

std::string myString("Hello World"); 
printf("* Debug: %s \n", myString);
Run Code Online (Sandbox Code Playgroud)

c++ printf

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

为什么 printf 输出的是字符而不是数据?

为什么 printf 输出的是字符而不是数据?\n看代码,相对能明白我要做什么,但是不太明白为什么输出是这样的

\n
#include <vector>\n#include <string>\n#include <cstdio>\n\nclass Person\n{\npublic:\n    Person(const std::string& name, uint16_t old)\n        : m_Name(name)\n        , m_Old(old) \n    {\n    }\n\npublic:\n    std::string GetName() const { return m_Name; }\n    uint16_t GetOld() const { return m_Old; }\n\nprivate:\n    std::string m_Name;\n    uint16_t m_Old;\n};\n\nint main()\n{\n    std::vector<Person> person = { Person("Kyle", 26), Person("Max", 20), Person("Josiah", 31) };\n    for (uint16_t i = 0; i < person.size(); ++i)\n    {\n        printf("Name: %s Old: %u\\n", person[i].GetName(), person[i].GetOld());\n    }\n    return 0;\n}\n\n\n> // output\n>     Name: \xd0\xac\xc2\xb7\xe2\x95\xa3 Old: 1701607755 \n>     Name: \xd0\xac\xc2\xb7\xe2\x95\xa3 Old: …
Run Code Online (Sandbox Code Playgroud)

c++ printf for-loop vector

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

标签 统计

c++ ×5

printf ×3

stl ×2

cout ×1

for-loop ×1

formatting ×1

performance ×1

stdstring ×1

string ×1

vector ×1