标签: std

std :: find在空向量上是否会导致未定义的行为?

试图找到一个信息,如果在std :: find期间使用了一个空向量,但找不到任何信息,那么会发生什么.

我的问题是,如果一个空向量传递给std :: find,返回值总是一个nullptr还是未定义的行为?

  std::vector<int> someDataContainer;
  auto it = std::find(someDataContainer.begin(), someDataContainer.end(), 1);
Run Code Online (Sandbox Code Playgroud)

c++ vector std c++11

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

std :: map中的项目是否永远保留在同一地址?

采取以下简单程序:

struct Foo
{
    int x;
    int y;
    int z;
    string s;
};

int main()
{
    Foo f1 = { 42,21,11, "Hello world" };
    std::map<int, Foo> foomap;

    foomap[400] = f1;
    Foo* ptr = &foomap[400]; // cache a pointer to the element we just inserted.

    cout << ptr->x << " " << ptr->y << " " << ptr->z << " " << ptr->s << std::endl;

    // fill the map up with a bunch of other random items at random indices   
    for …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap std

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

使用std :: move将unique_ptr移动到向量中

我一直在尝试创建一个unique_ptr,然后将其移动到该指针的向量中push_back().当我尝试这样做时,我得到一个很长的编译错误.我已经阅读了有关此主题的多个问题,包括"unique_ptr的容器"部分:https://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers

以及这个StackOverflow问题:为什么我不能将unique_ptr push_back到向量中?

这是一个无法编译的小样本程序:

// ptrtest.cpp
#include <memory>
#include <vector>

class TestObject{
public:
    TestObject(int data): data(data){}
    int getData(){return data;}
private:
    int data;
};

using namespace std;
int main (int argc, char* argv[]){
    vector<unique_ptr<TestObject> > v;
    unique_ptr<TestObject> obj = unique_ptr<TestObject>(new TestObject(5));
    v.push_back(move(obj));

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

这是程序给我的编译器错误:

$ clang++ ptrtest.cpp -otest
In file included from ptrtest.cpp:1:
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1574:36: error: 
      no matching constructor for initialization of
      'std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
                ::new ((void*)__p) _Tp(__a0);
                                   ^   ~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1593:25: note: in …
Run Code Online (Sandbox Code Playgroud)

c++ vector std unique-ptr c++11

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

在调用clear时,thread_local std :: list是否放弃分配的内存吗?

当我们调用std::list还带有标记的类型的变量clear时会发生什么thread_local?它会放弃分配的内存吗?

在我的场景中,我有一个thread_local列表,该列表在一次迭代中填充,并在迭代结束时清除。thread_local在我们在迭代结束时放弃内存(并且必须在下一次迭代中再次分配内存)的情况下,这不是很有用吗?

c++ memory-management std stdlist c++11

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

编译器生成的默认构造函数是否将std :: array中的指针初始化为nullptr?

给定下面的代码是否正确,编译器应生成Node()哪个应调用std::array<Node *, 100>()哪个应初始化所有指向nullptr的100指针。

旁注:我知道如果使用std::array<Node *, 100> children {};,我可以做到这一点,但我并不是想让我的代码正常工作(它已经做到了),而是试图确保它不是偶然地工作。

struct Node
{
    int value;
    std::array<Node *, 100> children;
}
Run Code Online (Sandbox Code Playgroud)

更新:

这里的指针是垃圾:

struct Node
{
    int value;
    std::array<Node *, 100> children;
}

struct Node
{
    Node() : children() {}
    int value;
    std::array<Node *, 100> children;
}
Run Code Online (Sandbox Code Playgroud)

这里的指针是nullptr:

struct Node
{
    int value;
    std::array<Node *, 100> children {};
}

struct Node
{
    Node() : children{} {}
    int value;
    std::array<Node *, 100> children;
}
Run Code Online (Sandbox Code Playgroud)

请改正我,如果我错了。

c++ arrays initialization std

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

如何在数组中间构造开始和结束迭代器?

我正在尝试使用需要两个迭代器的std :: reverse将数组的一部分反转到位。当我尝试如下构造迭代器时:

int a[] = {0, 1, 2, 3, 4, 5, 6};
reverse(begin(a + 2), end(a + 4));
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

template argument deduction/substitution failed:
test.cpp:8:33: note:   mismatched types ‘const std::valarray<_Tp>’ and ‘int*’ reverse(begin(a + 2), end(a + 4));
Run Code Online (Sandbox Code Playgroud)

构造迭代器的正确方法是什么?

c++ reverse iterator std

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

带有原始指针的唯一指针.at()的映射

说我有一张地图:

std::map<std::unique_ptr<SomeType>, SomeOtherType> map;
Run Code Online (Sandbox Code Playgroud)

显然,这是行不通的,因为我们地图的键值是唯一的ptr,而不是原始的:

//a pointer from somewhere else in the code
SomeType* p = ...;
auto result {map.at(p)};
Run Code Online (Sandbox Code Playgroud)

相反,可以使用std :: unique_ptr.get()执行以下操作:

SomeType* p = ...;
for(auto& entry : map) {
    if(entry.first.get() == p) {
        //do whatever
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这是一种非常丑陋且可能效率不高的方法。我的问题很简单,在这种情况下是否有某种方式可以使用.at()函数。

c++ dictionary pointers std unique-ptr

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

std :: chrono :: from_stream正确用法

我正在考虑使用函数从like std::chrono::from_stream (std::chrono::year_month_day)获取std::chrono::year_month_day对象。const std::string&2018-12-09T00:00:00

在cppreference中没有用法的示例std::chrono::from_stream。所以我想,它可以像一样使用std::get_time。

但是有一个问题!std::chrono::year_month_day有非常奇怪的参数(个人意见)

(std::basic_istream<CharT, Traits>& is, const CharT* fmt, std::chrono::year_month_day& ymd, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr, std::chrono::minutes* offset = nullptr)
Run Code Online (Sandbox Code Playgroud)

然后出于未知原因返回std::basic_istream,因此我意识到我真的不知道如何正确使用它。

有谁知道吗?谢谢。

c++ std c++-chrono c++20

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

Fail to compile: strlen is not a member of std

I'm trying to compile this very simple program where I'm implementing a simplified version of C++ strings. However the compiler cannot find the std::strlen function even though I included

//main.cpp
#include "str.h"

int main()
{
    return 0;
}

// str.h
#ifndef _STRING_H_
#define _STRING_H_

#include <vector>
#include <cstring>
#include <algorithm>
#include <iterator>

class Str {
public:
    typedef std::vector<char>::size_type size_type;

    Str() { }
    Str(size_type n, char c): data(n, c) { }
    Str(const char* cp) {
        std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
    }
    template <class …
Run Code Online (Sandbox Code Playgroud)

c++ std strlen

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

尝试取消引用迭代器时出现段错误

我正在尝试从向量中获取minmax元素。下面是我的代码的过度简化的片段:

std::vector<int> vec;
for (int i = 1; i < 10; i++) {
  vec.push_back(i);
}
auto minmax = std::minmax(vec.begin(), vec.end());
int min_value = *minmax.first;
Run Code Online (Sandbox Code Playgroud)

当我尝试在最后一条语句中取消引用迭代器时,出现段错误。我不明白为什么。

c++ iterator std

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