标签: std

如何返回标准文件流

我正在为我的应用程序开发一个“文件系统”来处理文件操作,例如列出、创建、删除文件和目录以及获取文件数据。我想为此使用 std::fstream ,因为它比 C FILE Handler 更安全、更容易格式化。但是我无法从函数返回流对象(它没有副本 c'tor)并且当我尝试返回引用时我收到警告(我不是警告并不意味着那么多,但我正在尝试修复我的应用程序中所有可能的警告,直到现在我都做了):“警告:返回对临时的引用”。我能做什么?如果有人遇到这种情况并找到了更好的处理方法,请告诉。

编辑:

std::ofstream &Filesystem::createFile(const String &str) {
   std::ofstream file(str);
   return file;
}
Run Code Online (Sandbox Code Playgroud)

这就是我正在努力实现的目标。但由于警告,我正在寻找另一种方式。

提前致谢,
Gasim Gasimzada

c++ g++ std

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

如何获取指向原始数据的 std::deque 指针?

我知道 std::vector、std::vector: data() 和 &something[0] 都可以,那么 std::deque 呢?std::deque 没有 data() 函数。

编辑:如果不能保证 std::deque 使用连续存储,我如何实现可以获取原始数据的双端队列?

c++ std

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

OpenMP - std::next_permutation

我正在尝试使用 OpenMP 并行化我自己的 C++ 实现的旅行商问题。

我有一个函数来计算道路cost()和向量 [0,1,2,...,N] 的成本,其中 N 是道路的节点数。

在main(),我试图找到最好的道路:

do
{
    cost();
} while (std::next_permutation(permutation_base, permutation_base + operations_number));
Run Code Online (Sandbox Code Playgroud)

我试图用来#pragma omp parallel并行化该代码,但它只会让它更耗时。有没有办法并行化该代码?

c++ std openmp traveling-salesman

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

UTF8 数据到 std::string 或 std::wstring

我从 HTTP 服务器响应接收正文字节,但我不知道如何将它们转换为 UTF8 字符串以使用它们。

我有一个想法,但我不确定它是否有效。我需要获取响应的字节并搜索它们并修改它们,因此我需要将std::vector<BYTE>tostd::wstring或std::string.

响应的 UTF8 字节编码在 my 中std::vector<BYTE>,如何将它们转换为std::string?我要不要把它们转换成std::wstring?。

我找到了这个代码:

std::string Encoding::StringToUtf8(const std::string& str)
{
INT size = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, str.c_str(), str.length(), NULL, 0);

std::wstring utf16_str(size, '\0');

MultiByteToWideChar(CP_ACP, MB_COMPOSITE, str.c_str(), str.length(), &utf16_str[0], size);

INT utf8_size = WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), utf16_str.length(), NULL, 0, NULL, NULL);

std::string utf8_str(utf8_size, '\0');

WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), utf16_str.length(), &utf8_str[0], utf8_size, NULL, NULL);

return utf8_str;
Run Code Online (Sandbox Code Playgroud)

}

但是现在,如果我想在字符串中搜索像“Ñ”这样的字符会起作用吗?,或者我是否要转换 a 中的字节std::wstring并搜索“Ñ”修改std::wstring并将其转换为std::string …

c++ string encoding std wstring

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

如何从一组派生类创建列表?

假设我有一组以下课程:

class animal
{
// members, constructors and other methods...

public:
    virtual void what_do_you_eat() { cout << "i eat generic food" << endl; }
}

class cat : public animal
{
// members, constructors and other methods...

public:
    void what_do_you_eat() { cout << "i eat cat food" << endl; }
}

class dog : public animal
{
// members, constructors and other methods...

public:
    void what_do_you_eat() { cout << "i eat dog food" << endl; }
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个列表(任何容器都可以,但让我们std::list<>为这个例子选择): …

c++ inheritance std

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

为什么在新的IDE中必须使用命名空间std,而用Turbo C ++ / Borland C ++编写的程序却不需要命名空间std?

为什么namespace std用Turbo C ++ / Borland C ++编写的程序不需要名称空间std 时必须在新的编译器中使用?

这适用于旧的编译器

#include <iostream.h>

int main () {
   cout << "Hello Programmers";

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

但是我们必须在新的编译器中编写以下给定的程序,而不是在上面的程序中编写,因为上述程序在新的编译器中不起作用。

#include <iostream>
using namespace std;

int main () {
   cout << "Hello Programmers";

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

c++ namespaces std turbo-c++

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

如何使用模板为“ std :: array”声明“ using”

例如,我有类似的代码:

template<typename A, typename B>
using Map = std::map<A, B>;

template<typename A, typename B>
using UnorderedMap = std::unordered_map<A, B>;
Run Code Online (Sandbox Code Playgroud)

我想对做同样的事情std::array,即:

template<typename A, typename B>
using Array = std::array<A, B>;
Run Code Online (Sandbox Code Playgroud)

但是,如果这样做,我将得到一个编译器错误:

错误C2993:'B':非类型模板参数'_Size'的非法类型

错误C2955:“ std :: array”:使用类模板需要模板

参数列表array(21):message:参见'std :: array'的声明

有什么方法可以声明Array在后台使用的std::array吗?

最后,我想使用Array<int, 7> items代替的数组std::array<int, 7> items。

c++ arrays templates std stdarray

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

语言的哪一部分禁止更改std :: set的元素

An std::set是一种分类的关联容器,可快速查找其元素。密钥以一种有序的方式插入,一旦插入就不能修改密钥以保持该顺序。

考虑下面的示例,该示例构造一个std::set,int*然后尝试破坏其元素的排序:

#include <iostream>
#include <set>

int values[] = { 50, 40, 30, 20, 10 };

// Comparator that sorts by pointed value
struct comparator {
    bool operator()(const int* left, const int* right) const {
        return *left < *right;
    }
};

using my_set_t = std::set<int*, comparator>;

// Checks if each of the elements of `values` are in the set
void output(const my_set_t & foo)
{
    for (auto & x : values) {
        std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ std invariants stdset language-lawyer

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

C++ 在地图上循环 - 消失的对象

所以,我在代码中发现了一个相当奇怪的错误,我很难理解。我有一个 std::map 用于存储一些信息。当我第一次循环时,一切看起来都很好,但是第二次,数据丢失了。

首先,我使用一个结构体和一个类,如下所示:

struct Eng::Pixel{
        unsigned int x;
        unsigned int y;
    };

class Eng::Edge{
public:
enum EdgeType { INTER, INTRA};
EdgeType type;
}

class Eng::Cluster{
public:
std::map<Pixel, std::vector<Edge>> trans;
}
Run Code Online (Sandbox Code Playgroud)

基本上,一个簇包含一张像素图。该地图中的每个像素都包含称为边缘的过渡点。每个像素可以有多个边缘 - 边缘可以是 inter(0) 或 intra (1) 类型。请注意,某些命名空间可能会丢失,因为我试图尽可能地简化我的问题。

当我循环代码时:

std::vector<Cluster> resClusters = this->GenerateClusters();

for(Cluster cluster : resClusters) //For a given cluster in clusters
        {
            this->CreateIntraEdges(cluster); //Create our intra edges. Succeeds.
            std::cout << "Cluster: " << cluster << std::endl; //Prints the bounds of the cluster.
            std::cout << "Cluster has " …
Run Code Online (Sandbox Code Playgroud)

c++ std

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

这个 C++ 代码如何在没有函数定义的情况下运行

任何人都可以解释这个程序中定义函数的工作背后的逻辑。即使在评论整个函数逻辑之后,这也会在这种情况下给出正确的输出。

#include <iostream>
using namespace std;
void swap(int *a, int *b)
{ /*
int temp=0;
 temp= *a;
 *a= *b;
 *b=temp;
*/
}
int main()
{
    int x, y;
    cout << "This program is the demo of function call by pointer \n\n";
    cout << "Enter the value of x & y \n";
    cout << "x: ";
    cin >> x;
    cout << "y: ";
    cin >> y;
    cout << "Value befor swap " << endl;
    cout << "x= " << x << …
Run Code Online (Sandbox Code Playgroud)

c++ swap std

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