标签: std

C++ 库中的任何内置钳位方法?

我想知道是否有一些内置的钳位方法可以钳制一个范围之间的值,比如在 (0,1) 之间?

clamp(a) = a if a is in (0,1)
a < 0 a = 0
a > 1 a = 1
Run Code Online (Sandbox Code Playgroud)

c++ std clamp

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

C++ 将 std 列表拆分为两个列表

嘿,所以我对 C++ 相当陌生,我遇到了这个问题,我想将一个字符串的 std 列表拆分为两个列表。

例如:list(1,2,3,4)->list1(1,2) & list2(3,4)

我想splice这就是我应该使用的,但我根本不明白它是如何工作的......

有人可以建议我如何做到这一点吗?
抱歉我的英语不好,感谢大家的帮助。

c++ list std

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

用于生成向量的STL算法

我想用STL算法生成一个矢量来实现以下功能

const int N1 = 10; // This can vary
const int offset = 3; // This also can vary
std::vector<int> chans(10);
for (size_t i = 0; i < chans.size(); i++)
{
    chans[i] = offset + N1*i;
}
Run Code Online (Sandbox Code Playgroud)

有什么建议?

c++ algorithm stl vector std

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

我不能做一个简单的仿函数来比较字符串

我有这个仿函数类:

#include <string>

using namespace std;

class IsPlayerOfType
{
    public:
        IsPlayerOfType(const string& type) : type_(type) {}

        bool operator()(const Player* player) const
        {
            return (player->getType() == type_);
        }
    private:
        string type_;
};
Run Code Online (Sandbox Code Playgroud)

"Player"类表示具有多种方法和属性的玩家.其中,有getType()方法返回一个字符串.

在我的程序的某些时候,我有一个名为players_type 的变量vector<Player*>

最后,我有以下代码来计算我的向量中某种类型的玩家数量:

int number = count_if(players_.begin(), players_.end(), IsPlayerOfType("Defensive"));
Run Code Online (Sandbox Code Playgroud)

编译时我遇到很多错误,例如:

  • 错误C2011:'IsPlayerOfType':'class'类型重定义
  • 错误C2440:'':无法从'const char [10]'转换为'IsPlayerOfType'
  • 错误C2780:'iterator_traits <_Iter> :: difference_type std :: count_if(_InIt,_InIt,_Pr)':需要3个参数 - 提供2个参数

    我不太了解count_if是如何工作的,我试着写这段代码从这个答案中激励自己:https://stackoverflow.com/a/13525420

    我没有看到我错在哪里,编译器错误让我感到困惑.

c++ std functor

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

迭代器begin()应该包含3,输出说2?

为什么指向列表开头的迭代器输出第二个值?为什么a.begin()++会提前begin()并且有更好的实现?

#include <iostream>
#include <list>
using namespace std;
//3,2,1
int main() {
    list<int> a;
    a.insert(a.begin(),1);              
    cout << *(a.begin()) << endl;
    a.insert(a.begin(),3);
    cout << *a.begin()<< endl;
    a.insert(a.begin()++,2);
    list<int>::iterator iterator = a.begin();
    iterator++;
    cout << *iterator << endl;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的输出:

1
3
3
Run Code Online (Sandbox Code Playgroud)

预期产量:

1
3
2
Run Code Online (Sandbox Code Playgroud)

编辑:"因为你把2放在列表的开头.请记住a.begin()++正在进行后递增,即在所有其他操作之后递增.使用++ a.begin()尝试你的代码并查看如果它符合你的期望" - @Ben

排版错误,谢谢Ben.

c++ stl std

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

我怎么知道std :: vector分配的内存大小?

我了解我们可以使用size()函数来获取向量大小,例如:

   std::vector<in> abc;
   abc.resize(3);
   abc.size();
Run Code Online (Sandbox Code Playgroud)

我的问题是如何知道向量的内存大小?举个例子:

std::vector<int> abc;
abc.reserve(7);
//the size of memory that has been allocated for abc
Run Code Online (Sandbox Code Playgroud)

c++ vector std

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

std :: make_shared <Type>会造成内存泄漏

当我使用std :: make_shared时,Valgrind给了我一些内存泄漏:

TEST_F(CTestChild, add_gran_child) {
    auto child{ std::make_shared<CChild>(TType::Home, std::make_shared<CMockParent>()) };
    NiceMock<CMockCaller> caller;
    auto gran_child( std::make_shared<CMockGranChild>(TType::Girl, child, caller) );
    child->Add(gran_child);
    EXPECT_EQ(child->GetCount(), 1);
}

class CMockParent : CParent{
public:
    void something(void) override {}
}

class CParent{
public:
    virtual void something(void) = 0;
}

class CChild{
public:
    CChild(TType, shared_ptr<CParent> a) : _parent(a) {}
    void Add(shared_ptr<CGranChild> a) { _list.push_back(a) }
    shared_ptr<CParent> _parent;
    TList<shared_ptr<CGranChild>> _list;
}

class CGranChild{
public:
    CGranChild(TType, shared_ptr<CChild> a) : i_parent(a) {}
    shared_ptr<CChild> _parent;
}
Run Code Online (Sandbox Code Playgroud)

为什么make_shared会给我一个内存泄漏?

编辑:我已经包含了类的摘要,以便更好地理解代码.

c++ smart-pointers std c++11

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

使用std :: find没有匹配的函数调用错误

我有以下功能(用于测试):

static bool foo(void)
{
  std::string name = "name";
  std::vector<std::string> test;
  std::vector<std::string>::iterator vStart = test.begin();
  std::vector<std::string>::iterator vEnd = test.end();
  return (std::find(vStart, vEnd, name) == vEnd);
}
Run Code Online (Sandbox Code Playgroud)

我收到编译错误:

/data/src/fiware-orion/src/lib/common/string.cpp: In function 'bool foo()':
/data/src/fiware-orion/src/lib/common/string.cpp:167:39: error: no matching function for call to 'find(std::vector<std::basic_string<char> >::iterator&, std::vector<std::basic_string<char> >::iterator&, std::string&)'
   return (std::find(vStart, vEnd, name) == vEnd);
                                       ^
/data/src/fiware-orion/src/lib/common/string.cpp:167:39: note: candidate is:
In file included from /usr/include/c++/4.9/bits/locale_facets.h:48:0,
                 from /usr/include/c++/4.9/bits/basic_ios.h:37,
                 from /usr/include/c++/4.9/ios:44,
                 from /usr/include/c++/4.9/istream:38,
                 from /usr/include/c++/4.9/sstream:38,
                 from /data/src/fiware-orion/src/lib/common/string.cpp:31:
/usr/include/c++/4.9/bits/streambuf_iterator.h:369:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, …
Run Code Online (Sandbox Code Playgroud)

c++ std stdvector

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

无法插入到std :: list中?

我正在尝试将值插入到void中调用的std :: list中.void将在从另一个函数调用时将值插入到列表中,但是当我尝试使用.push_back,.Insert或.Add将值插入到列表中时,我收到以下错误:我收到以下错误

这是我的代码:

void pb(std::list<unsigned long long int> primeFactorisation(unsigned long long int), int n)
{
    for (int i=n; i<n; i++)
    {
        primeFactorisation.push_back(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白我做错了什么,这是一个标准清单?

c++ std

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

返回std :: nullopt作为非恒定引用

如果我想返回std :: nullopt作为非恒定引用,请形成学术观点。我将如何做同样的事情。

没什么背景,今天当我在处理返回std :: optional>引用的代码时,却忘记了使返回常量,并得到了错误。

Error (active)    E0434   a reference of type "std::optional<std::vector<std::any, std::allocator<std::any>>> &" (not const-qualified) cannot be initialized with a value of type "const std::nullopt_t"
Socket.IO   D:\Hardware\Windows\Visual Studio\Socket.IO\Socket.IO\main.cpp  46  

Error C2440   'return': cannot convert from 'const std::nullopt_t' to 'std::optional<std::vector<std::any,std::allocator<_Ty>>> &'
Socket.IO   d:\hardware\windows\visual studio\socket.io\socket.io\main.cpp  46
Run Code Online (Sandbox Code Playgroud)

只是想知道是否有人想使用std :: optional返回一个非常量引用,他将如何做。

使用平台:Windows 10 Pro x64

开发环境:Visual Studios 15.9.9

std::vector<int>> a;
std::optional<std::vector<int>>& test(int b)
{
    a.clear();
    a.push_back(b);
    if(b)
         return a;
    else
         return std::nullopt;
}
Run Code Online (Sandbox Code Playgroud)

c++ std c++17 stdoptional

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

标签 统计

c++ ×10

std ×10

stl ×2

vector ×2

algorithm ×1

c++11 ×1

c++17 ×1

clamp ×1

functor ×1

list ×1

smart-pointers ×1

stdoptional ×1

stdvector ×1