草庐IT

std-ranges

全部标签

c++ - 使用 std::transform 将字符串转换为所有大写字母

我正在使用转换算法和std::toupper来实现这一点,但是这可以像这样在一行中完成吗?transform(s.begin(),s.end(),ostream_iterator(cout,"\n"),std::toupper);我在这方面遇到了错误,所以我是否必须为此创建一个一元函数并使用转换调用它,或者我可以使用一些适配器? 最佳答案 使用ostream_iterator而不是ostream_iterator:transform(s.begin(),s.end(),ostream_iterator(cout,"\n"),std:

c++ - 清除 std::vector 需要赋值运算符。为什么?

在我的应用程序中,我需要存储一小部分临时数据。在这个临时数据中,我想存储对另一个类的引用,因为它不能是nullptr,所以我使用了一个引用。使用vector来存储数据(我没有太多数据,所以vector很好)。填充vector并对其进行迭代工作正常,但清除vector似乎会产生问题。这是一些显示问题的简化代码:classDepartment{};classPerson{public:Person(constDepartment&dept):m_dept(dept),m_salary(1000){}private:constDepartment&m_dept;doublem_salary;

c++ - std::queue<T, list<T>>::size() 在 O(n) 中很慢?

我在使用队列的代码中遇到了意外的性能行为。我意识到当队列中有更多元素时性能会下降。事实证明,使用size()方法是原因。这是一些显示问题的代码:#include#include#include#include"Stopwatch.h"usingnamespacestd;structBigStruct{intx[100];};intmain(){CStopwatchqueueTestSw;typedefBigStructQueueElementType;typedefstd::queue>QueueType;//typedefstd::queueQueueType;//nosurpris

c++ - 如何用默认值填充 `std::vector<std::vector<T>>`?

所以我试试这个:std::vector>matrix(4);matrix[0][0]=1;matrix[0][1]=2;matrix[0][2]=3;matrix[0][3]=1;matrix[1][0]=1;matrix[1][1]=2;matrix[1][2]=3;matrix[1][3]=1;matrix[2][0]=1;matrix[2][1]=2;matrix[2][2]=3;matrix[2][3]=1;matrix[3][0]=1;matrix[3][1]=2;matrix[3][2]=3;matrix[3][3]=1;但是出了点问题,我的应用程序在运行时死机了=(怎么办

c++ - 如何使用 std::mem_fun 传递参数

我想知道是否可以使用std::mem_fun传递参数?我想准确地说,我可以有尽可能多的参数和很多成员函数。问题是我使用的是旧标准,我正在寻找一种完整的STL方式,因此即使我知道我可以轻松做到,也不允许将boost作为答案=/这是我想如何使用它的一个小例子:#include#include//Classdeclaration//structInterface{virtualvoidrun()=0;virtualvoiddo_something(int)=0;virtualvoiddo_func(int,int)=0;};structA:publicInterface{voidrun(){

c++ - 从 std::list 中清除元素的顺序是什么?

我想清除一些std::list的内容。删除元素的顺序对我来说很重要。根据以下测试程序的输出,顺序是从第一个元素到最后一个元素。保证是这样吗?从C++2003标准来看,我并不清楚。#include#includestructA{A(inti):I(i){}~A(){std::coutl;l.push_back(A(1));l.push_back(A(2));l.push_back(A(3));std::coutideonelink 最佳答案 不,它没有定义,你不应该依赖它。 关于c++-从

c++ - std::set 在运行时选择更少或更大的比较器

我重构了一些代码,发现除了集合的比较器是less之外,有两个地方可以用相同的代码编写。在一个地方和greater在另一个。像这样的东西:doubleMyClass::Function1(doubleval){std::set>s;//Dosomethingwiths}doubleMyClass::Function2(doubleval){std::set>s;//DothesamethingwithsasinFunction1}所以我想到了:doubleMyClass::GeneralFunction(doubleval,boolcondition){if(condition){//S

c++ - 如何对 std::shared_ptr<Widget> 对象的容器进行排序?

classWidget;std::vector>containerclassCriterium{public:booloperator()(constWidget&left,constWidget&right)const;};如何根据标准对容器进行排序,无需定义另一个标准,例如:classCriteriumForPointers{public:booloperator()(conststd::shared_ptr&left,conststd::shared_ptr&right)const;}; 最佳答案 您可以使用lambda作为适

c++ - std::thread Visual Studio 2012 警告

我试图了解如何通过VisualStudio2012使用新的std::thread。我正在尝试编译以下代码。#include#includeclassscoped_thread{std::threadt_;public:explicitscoped_thread(std::thread&t):t_(std::move(t)){if(!t_.joinable())throwstd::logic_error("Nothread");}~scoped_thread(){t_.join();}private:scoped_thread(scoped_threadconst&);scoped_th

c++ - 为什么 std::sort 不接受函数内声明的比较类

我当时正在工作,在一个函数中编写比较器(稍后移动,当我决定最好的地方时),并注意到了这个特性。我想了一会儿,意识到我不明白为什么如果我使用内部比较器代码将无法编译,但外部比较器很好。有什么解释吗?快速测试工具:#include#include#includeclassCompareMe{public:CompareMe(intin):toCompare(in){}inttoCompare;};classComparators{public:booloperator()(CompareMe*first,CompareMe*second){returnfirst->toComparetoC