我正试图用cxx-11's std::unique()来找到一个独特的元素array:
#include <iostream>
#include <algorithm>
#include <vector>
#include <typeinfo>
int main(){
const int n=11;
double x[n],a3[n],a1[n];
x[0]=-0.717778;
x[1]=-0.496843;
x[2]=-0.429063;
x[3]=-0.3596;
x[4]=-0.205607;
x[5]=0.0730536;
x[6]=0.138018;
x[7]=0.585526;
x[8]=2.40104;
x[9]=3.75268;
x[10]=4.55704;
a3[0]=0.790832;
a3[1]=0.569896;
a3[2]=0.502116;
a3[3]=0.432653;
a3[4]=0.343625;
a3[5]=0.512472;
a3[6]=0.56708;
a3[7]=1.01459;
a3[8]=2.32799;
a3[9]=3.67962;
a3[10]=4.48398;
std::cout.precision(10);
std::copy(a3,a3+n,a1);
for(int i=0;i<n;i++) a1[i]+=x[i];
std::sort(a1,a1+n);
for(int i=0;i<n;i++) std::cout << a1[i] << std::endl;
std::cout << "---" << std::endl;
int n_1=std::unique(a1,a1+n)-a1;
std::cout << "length of unique subvector " << n_1 << std::endl;
std::cout << "---" << std::endl; …Run Code Online (Sandbox Code Playgroud) 这一切都在标题中.
请考虑以下代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <typeinfo>
using namespace std;
int main(){
const int n=12;
double x[n];
x[0]=-0.717778;
x[1]=-0.496843;
x[2]=-0.429063;
x[3]=-0.3596;
x[4]=-0.205607;
x[5]=0.0730536;
x[6]=0.138018;
x[7]=0.585526;
x[8]=2.40104;
x[9]=3.752680001; //here
x[10]=3.75268;
x[11]=4.55704;
std::cout << std::is_sorted(x,x+n) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想它返回1(真正的):虽然我可以看到,x[9] 是大于x[10],它们之间的差值小于1e-8
所以我带他们来是平等的.
也许这个问题有点理论,但我不知道什么是落后定义设计激励std::minmax这样的
template <class T>
pair<T,T> minmax (initializer_list<T> il);
Run Code Online (Sandbox Code Playgroud)
这意味着,IMO(传递的对象)li将被复制,其每个成员也必须是可复制构造的.
虽然,std::min_element(或者就此而言std::max_element)在某种意义上更加"有效",但只传递容器迭代器(不需要实际复制整个容器)
template <class ForwardIterator>
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);
Run Code Online (Sandbox Code Playgroud)
编辑 - 基于Joachim Pileborg评论,initializer_list<T>对象没有被复制,所以我正在查明我的问题 - 为什么std::minmax被约束到这样的对象而不是任意容器(具有"非const"性质,可以这么说)
我有一个A可以响应的类doSomething()和一些子类B,C并且D都覆盖了doSomething().B特别是有一个实例变量,它是标准库中的列表,并在响应时将元素添加到这样的列表中doSomething().
在我的代码的某些部分,我创建了一个包含指向类型对象的指针的数组A.现在,如果我这样声明:
A* pointersToA[3];
pointersToA[0] = &B();
pointersToA[1] = &C();
pointersToA[2] = &D();
Run Code Online (Sandbox Code Playgroud)
我List insert iterator outside range打电话的时候得到了pointersToA[0]->doSomething().
但如果我这样做:
A* pointersToA[3];
B b = B();
pointersToA[0] = &b;
pointersToA[1] = &C();
pointersToA[2] = &D();
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作.
为什么会这样?
有什么区别
MyClass* myClass = new MyClass;
std::thread myThread( &MyClass::MyMemberFunction, myClass );
Run Code Online (Sandbox Code Playgroud)
和
std::thread( &MyClass::MyMemberFunction, myClass );
Run Code Online (Sandbox Code Playgroud)
?
PS尝试使用第二种方法将boost :: asio :: io_service放入单独的线程 - 不起作用.但第一个确实如此.如果改变std以提升,两种方法都有效.
WIndows 7. MSVS 12.0.
我正在尝试扩展别人的代码,它经常使用std :: queue和std :: pair类型.对于下面的代码,我需要扩展std :: pair.second中的变量数量,并希望它对于像数组这样的变量数量更加灵活(以便将来修改).
someClass->myQueue.push(std::pair<T1,uint64_t>(var1, var2));
Run Code Online (Sandbox Code Playgroud)
所以我尝试做类似的事情:
someClass->myQueue.push(std::pair<T1,uint64_t[N]>(var1,{e1,e2,...,eN}));
Run Code Online (Sandbox Code Playgroud)
在修改代码中的相关定义等后,我得到了以下错误和警告,我对此一无所知.然而,听起来这不是进行这种修改的适当方式.
myArray = myQueue.front().second;
^
error: invalid array assignment
...
someClass->myQueue.push(std::pair<T1,uint64_t[N]>(var1,{e1,e2,...,eN}));
^
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
Run Code Online (Sandbox Code Playgroud)
我在网上看到了元组和std :: pair作为解决方案的递归用法,但我想对代码进行最小的更改,因为几个类将受此影响.这样做的正确方法是什么?
谢谢
请注意以下示例:
#include <iostream>
#include <functional>
#include <cstdlib>
void Print_Wrapper(std::function<void(int)> function);
void Print(int param);
int main(){
Print_Wrapper(Print);
return EXIT_SUCCESS;
}
void Print_Wrapper(std::function<void(int)> function){
int i = 5;
function(i);
}
void Print(int param){
std::cout << param << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这可以正常工作,并打印5.
现在看一下添加了重载函数的相同示例:
#include <iostream>
#include <functional>
#include <cstdlib>
void Print_Wrapper(std::function<void(int)> function);
void Print(int param);
void Print(float param);
int main(){
Print_Wrapper(Print);
return EXIT_SUCCESS;
}
void Print_Wrapper(std::function<void(int)> function){
int i = 5;
function(i);
}
void Print(int param){
std::cout << param << std::endl;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用擦除删除成语:
template <typename T>
bool garbageCollectVector(std::vector<T>& v) {
// Use the erase-remove idiom in combination with a lambda expression
v.erase(
std::remove_if(v.begin(), v.end(),
[this](const T& elem) -> bool {
return this->shouldRemove(elem);
}
),
v.end());
return /* what to return? */;
}
Run Code Online (Sandbox Code Playgroud)
并且想要返回该方法是否实际删除了任何元素.什么是干净办呀?
我有一个vector<Foo>和一些函数double toDouble(const Foo& foo),我想在向量中的所有Foos上运行toDouble函数后找到最小的double值.
使用for循环解决这个问题很容易,但出于好奇,有没有一种方法可以使用标准库算法来实现这一点?
一个想法是用来std::transform转换vector<Foo>为a vector<double> 然后std::min_element用来找到最小的double值,但这需要填充一个新的临时向量...是否有更直接的解决方案?
如果有向量,我们需要找到具有相同条件的多个项目.如果我们调用std::find_if它将返回条件的第一次出现.
std::vector <int> List{0,1,2,3,4,5,6};
auto item = find_if(List.begin(), List.end(), [](int x)
{
return x > 2;
}
);
Run Code Online (Sandbox Code Playgroud)
我知道我们可以使用std::for_each并将条件和动作放在lambda表达式中,但我问是否有一个方法可以自己完成并返回找到的项目的向量.