标签: std

为什么我可以在不使用 std::move 的情况下将 std::unique_ptr 复制到另一个?

除非您使用 std::move,否则不应将 std::unique_ptr 复制到另一个 std::unique_ptr,对吗?

但我经历过这样的事情。

#include <iostream>
#include <memory>

using namespace std;


class ClassA{
public:

    ClassA() {
        cout << "created" << endl;
    }

    ~ClassA() {
        cout << "destroyed" << endl;
    }



    void print() {
        cout << "ok" << endl;
    }

};

void test() {

    unique_ptr<ClassA> b(new ClassA());

    b->print();

    // unique_ptr<ClassA> d(b); this won't compile since you can't copy unique_ptr

    ClassA& aa = *b;  // ClassA aa = *b; works too.

    unique_ptr<ClassA> d(&aa); // but this can, why is that …
Run Code Online (Sandbox Code Playgroud)

c++ std unique-ptr

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

当我使用带有重音字母的 std::isupper() 时我该怎么办

我必须检查字符串的第一个字符是小写还是大写。

当我使用像 David 这样的英文名字时它很好用,但是当一个带有重音字母的名字弹出时,比如 Á,É,Í,Ó 等......(比如 Árpád)然后它认为它是小写的。

string name;
std::cin>>name;

if(std::isupper(name[0]))
{
std::cout<<"Upper case"<<std::endl;
}
else
{
std::cout<<"Lower case"<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ if-statement std

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

用不同的线程填充 std::vector

我需要在不同的线程上填充一个 std::vector。

它是正确的代码吗?或者我应该为我的代码添加互斥锁?

void func(int i, std::vector<float>& vec)
{
    vec[i] = i;
}

int main()
{
    std::vector<float> vec(6);
    std::list<std::thread> threads;
    for (int i = 0; i < 6; i++)
    {
        threads.push_back(std::thread(func, i, std::ref(vec)));
    }
    for (auto iter = threads.begin(); iter != threads.end(); iter++)
    {
      (*iter).join();
    }
}
Run Code Online (Sandbox Code Playgroud)

我测试了我的代码它工作正常。有什么陷阱吗?它是线程安全的代码吗?

如何通过不同的线程获取 std::vector 数据?

c++ std stdvector

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

对 std::vector 使用嵌套 [ ] 操作

我对 C++ 很陌生,我曾尝试寻找这个问题的答案并运行测试,但很多时候我无法弄清楚是什么导致了特定行为。我的问题与使用嵌套[ ]运算符访问或修改循环中的元素有关 - 例如:

//Declare

std::vector<int> a1 {10,20,30,40} ;
std::vector<int> a2 {2,3} ;
int S2 = a2.size() ; 

//Loop
for(int i = 0 ; i < S2 ; i++){
         a1[a2[i]] = a1[a2[i]] + 5000 ;
}
Run Code Online (Sandbox Code Playgroud)

这被认为可以吗?我不仅要问一般做法,还要问效率和我需要考虑的任何其他潜在因素。我是否应该首先存储a[i]在循环内的临时变量中,然后使用它来修改 vector 中的元素a2?

我确实知道它可能不是最好的结构,我应该使用其他一些数据结构来做这种事情,但我只想了解这是否可以或者是否可能导致一些未定义的行为。

c++ std stdvector

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

如何在 C++ 中使用 std::accummulate 计算总和立方体

假设我有一个范围,我想计算每个元素的立方体总和,例如[1,2,3]我想要的:

1 3 + 2 3 + 3 3 = 36

我将如何使用std::accummulate(),我必须编写自己的二进制函数来做到这一点?我有:

std::accummulate(iterator to beginning,
                 iterator to end of range,
                 0.0,
                 [](int i, int j) { return i*i*i + j*j*j; });
Run Code Online (Sandbox Code Playgroud)

但这由于某种原因不起作用。

问题就出来了不可思议,但在我的回报,我只是乘以i并j用自己3次加入。

c++ stl std range

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

std:find() 用于排序与未排序

考虑两个整数向量v1和v2,其中v1已排序和v2未排序。使用 搜索这两个向量中的元素的时间复杂度是find()多少?

c++ algorithm std

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

为什么这个简单的元组自定义比较器即使使用严格弱排序也会崩溃?

这个简单的自定义比较器用于tuple<int, int, int>下面的示例测试的类型崩溃。我检查cout了cmp比较器中的语句,每次调用cmp都会得到一个返回值,所以它不像元组 t1 和 t2 中的值是垃圾。

知道为什么会崩溃吗?这个非常简单的自定义比较器有什么问题吗?

class Solution {
public:
    vector<int> assignBikes(vector<vector<int>>& ws, vector<vector<int>>& bs) {
        int n = ws.size(), m = bs.size();        
        vector<tuple<int, int, int>> dist;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int d = abs(ws[i][0]-bs[j][0]) + abs(ws[i][1]-bs[j][1]);
                dist.push_back(make_tuple(d, i, j)); 
            }
        }     
        sort(dist.begin(), dist.end(), cmp());
    }
    
    struct cmp {
         bool operator() (tuple<int, int, int>& …
Run Code Online (Sandbox Code Playgroud)

c++ sorting std comparator custom-compare

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

如何有条件地编写依赖于 C++ 中 lambda 返回类型的代码?

我有为它迭代的每个元素调用回调的函数:

#include <type_traits>
#include <iostream>
using namespace std;

void iterate( const auto & on_element ) {
    for ( int i = 0; i < 10; ++ i ) {
        //here I want to stop iteration if function returns bool true:
        if constexpr ( is_same< result_of( on_element )::type, bool >::value )
            if ( ! on_element( i ) )
                return;
        //but when callback doesn't return bool - simply iterate the whole range:
        else
            on_element( i );
    }
}

int main() {
    const …
Run Code Online (Sandbox Code Playgroud)

c++ templates std

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

std::optional 不编译

此代码不能使用命令 g++ -std=c++17 main.cpp 编译

#include <iostream>
#include <experimental/optional>

int main()
{
    std::optional<int> x;
    std::cout << "Hello World";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误如下:

  1. 错误:“可选”不是“标准”的成员
  2. 错误:'int' 之前的预期主表达式

有没有办法让这段代码编译?

c++ std stdoptional

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

不了解 C++ stack&lt;char*&gt; 行为

我被困了两个小时试图了解这个简单的 C++ 测试程序中发生了什么,但仍然没有明白。它应该只接收三个字符串作为输入,将它们插入一个堆栈,最后打印该堆栈的所有元素。

#include <iostream>
#include <stack>
#include <cstring>

using namespace std;

int main(){
    
    stack<char*> stk;
    
    int stringLength;
    
    for (int i=0; i<3; i++){
        char new_element[200];
        scanf("%s", new_element);
        stringLength = strlen(new_element);
        stk.push(new_element);
    }
    
    cout << "Stack content: ";
    while(!stk.empty()){
        cout << stk.top() << " ";
        stk.pop();
    }
    cout << endl;

}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,最终输出的是同一个元素(最后添加的)打印了 3 次,这对我来说毫无意义。

例如,如果输入是:

John
Mary
Rick
Run Code Online (Sandbox Code Playgroud)

那么当前的输出是

Rick
Rick
Rick
Run Code Online (Sandbox Code Playgroud)

谁能帮我理解和解决这个问题?

c++ std stdstack

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