标签: standard-library

将整个ASCII文件读入C++ std :: string

我需要将整个文件读入内存并将其放在C++中std::string.

如果我把它读成a char[],答案很简单:

std::ifstream t;
int length;
t.open("file.txt");      // open input file
t.seekg(0, std::ios::end);    // go to the end
length = t.tellg();           // report location (this is the length)
t.seekg(0, std::ios::beg);    // go back to the beginning
buffer = new char[length];    // allocate memory for a buffer of appropriate dimension
t.read(buffer, length);       // read the whole file into the buffer
t.close();                    // close file handle

// ... Do stuff with buffer here ...
Run Code Online (Sandbox Code Playgroud)

现在,我想做同样的事情,但是使用a std::string而不是a char[] …

c++ string file-io caching standard-library

559
推荐指数
5
解决办法
49万
查看次数

如何检查Go中是否存在文件?

Go的标准库没有专门用于检查文件是否存在的函数(如Python os.path.exists).这样做的惯用方法是什么?

file standard-library go

393
推荐指数
14
解决办法
22万
查看次数

什么是std :: promise?

我相当熟悉C++ 11的std::thread,std::asyncstd::future部件(例如见这个答案),这是直接的.

但是,我不能完全理解std::promise它是什么,它做什么以及在哪种情况下最好使用它.标准文档本身不包含其类概要之外的大量信息,也不仅仅是:: thread.

有人可以给出一个简短,简洁的例子,说明std::promise需要哪种情况以及最惯用的解决方案?

c++ multithreading standard-library promise c++11

367
推荐指数
7
解决办法
8万
查看次数

从Swift中删除数组中的重复元素

我可能有一个如下所示的数组:

[1, 4, 2, 2, 6, 24, 15, 2, 60, 15, 6]

或者,实际上,任何类似数据类型的序列.我想要做的是确保每个相同的元素只有一个.例如,上面的数组将变为:

[1, 4, 2, 6, 24, 15, 60]

请注意,删除了重复的2,6和15,以确保每个相同的元素只有一个.Swift是否提供了一种轻松完成此操作的方法,或者我自己必须这样做?

arrays standard-library swift

222
推荐指数
22
解决办法
13万
查看次数

168
推荐指数
2
解决办法
3万
查看次数

为什么标准C++库中没有int int(int base,int exponent)?

我觉得我必须无法找到它.有什么理由说c ++ pow函数没有为浮点数和双精度数以外的任何东西实现"幂"函数吗?

我知道实现是微不足道的,我只是觉得我正在做一个应该在标准库中的工作.强大的幂函数(即以某种一致,明确的方式处理溢出)编写起来并不好玩.

c++ math integer standard-library pow

107
推荐指数
7
解决办法
6万
查看次数

为什么从函数返回向量是可以的?

请考虑此代码.我已经多次看过这种类型的代码了.words是一个本地向量.如何从函数中返回它?我们可以保证它不会死吗?

 std::vector<std::string> read_file(const std::string& path)
 {
    std::ifstream file("E:\\names.txt");

    if (!file.is_open())
    {
        std::cerr << "Unable to open file" << "\n";
        std::exit(-1);
    }

    std::vector<string> words;//this vector will be returned
    std::string token;

    while (std::getline(file, token, ','))
    {
        words.push_back(token);
    }

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

c++ scope stl vector standard-library

106
推荐指数
4
解决办法
13万
查看次数

Eclipse CDT for C标准库头文件中存在"未解决的包含"错误

我为eclipse设置了CDT并编写了一个简单的hello world C程序:

#include <stdio.h>

int main(void){
    puts("Hello, world.");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序正确构建和运行,但是eclipse在包含语句的一侧不断显示这个黄色问号,"Unresolved inclusion: <stdio.h>"当我把鼠标悬停在它上面时.

它不会影响程序的运行,但我觉得它很烦人.

有谁知道如何删除它?

c eclipse eclipse-cdt standard-library

95
推荐指数
5
解决办法
24万
查看次数

必须(应该)避免使用标准库中的哪些功能?

我已经读过Stack Overflow,有些C函数是"过时的"或"应该避免".你能告诉我一些这种功能的例子和原因吗?

这些功能有哪些替代方案?

我们可以安全地使用它们 - 任何好的做法?

c standard-library obsolete

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

连接字符串不能按预期工作

我知道这是一个常见的问题,但是在寻找参考资料和其他材料时,我找不到这个问题的明确答案.

请考虑以下代码:

#include <string>

// ...
// in a method
std::string a = "Hello ";
std::string b = "World";
std::string c = a + b;
Run Code Online (Sandbox Code Playgroud)

编译器告诉我它找不到重载的运算符char[dim].

这是否意味着在字符串中没有+运算符?

但在几个例子中,存在类似这样的情况.如果这不是连接更多字符串的正确方法,那么最好的方法是什么?

c++ string-concatenation standard-library stdstring operator-keyword

84
推荐指数
4
解决办法
22万
查看次数