我对这个std::get()
功能很困惑.std::get()
可以用来访问成员array
,pair
和tuple
.那么,为什么标准不允许它访问成员vector
呢?
#include <iostream>
#include <array>
#include <vector>
#include <tuple>
#include <utility> // std::pair
using namespace std;
int main()
{
array<int, 4> a1{3,4,5,67};
pair<int,int> p1{5,6};
tuple<int,float,float> t1{6,5.5,4.5};
cout << std::get<1>(a1) <<endl;
cout << std::get<1>(p1) <<endl;
cout << std::get<1>(t1) <<endl;
}
Run Code Online (Sandbox Code Playgroud)
以下是输出:
4
6
5.5
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用std::get()
时vector
,我得到了这个编译错误:
#include <iostream>
#include <array>
#include <vector>
#include <tuple>
#include <utility> // std::pair
using namespace std;
int main()
{
vector<int> v1{4,5,6,7,9}; …
Run Code Online (Sandbox Code Playgroud) 我多次运行以下代码,但为什么前缀增量的结果,fetch_add()显示正确的结果,而使用添加操作(+),它会输出错误的结果?
#include <iostream>
#include <mutex>
#include <future>
using namespace std;
atomic <int> cnt (0);
void fun()
{
for(int i =0; i <10000000 ; ++i)
{
//++cnt; // print the correct result 20000000
//cnt = cnt+1; // print wrong result, arbitrary numbers
cnt.fetch_add(1); // print the correct result 20000000
}
}
int main()
{
auto fut1 = async(std::launch::async, fun);
auto fut2 = async(std::launch::async, fun);
fut1.get();
fut2.get();
cout << "value of cnt: "<<cnt <<endl;
}
Run Code Online (Sandbox Code Playgroud) 请有人帮助我理解"streambuf,stringbuf和stringstream"之间的区别.
void f1(std::string const& text)
{
std::stringstream inStream(text);
cout<<inStream.str()<<endl;
}
Run Code Online (Sandbox Code Playgroud)
或者如果我写
void f2(std::string const& text)
{
std::stringbuf inStream(text);
cout<<inStream.str()<<endl;
}
Run Code Online (Sandbox Code Playgroud)
两者都显示相同的结果.我什么时候应该使用stringbuf或stringstream?提前致谢.
我在下面的代码中得到运行时错误.请让我知道我可以复制集合中的矢量元素吗?
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main()
{
vector<int> v;
set<int> kk;
set<int>::iterator itr;
for (int i = 0; i < 6; i++)
{
v.push_back(i * 2);
}
copy(v.begin(), v.end(), inserter(kk, itr));
}
Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
class base
{
int a;
public:
base() {a =0;}
};
class derv :public base
{
int b;
public:
derv() {b =1;}
};
int main()
{
base *pb = new derv();
delete pb;
}
Run Code Online (Sandbox Code Playgroud)
我在derv类中没有虚拟析构函数,它只删除了derv对象的基本部分吗?
c++ polymorphism dynamic-memory-allocation virtual-destructor
Scott Meyers说:
C++指定始终复制作为异常抛出的对象,并由对象的复制构造函数执行复制.
但在我的代码中:
struct test
{
test() { cout << "constructor is called" << endl; }
test(const test&) { cout << "copy constructor is called" << endl; }
~test() { cout << "destructor is called" << endl; }
};
void fun()
{
throw test();
}
int main()
{
try {
fun();
}
catch (test& t1) { cout << "exception handler" << endl; }
}
Run Code Online (Sandbox Code Playgroud)
我没有看到异常对象的复制构造函数被调用.
如果我更改catch
按值接收异常对象,那么它是,但根据Meyers的引用,异常对象应该被复制,即使它是通过引用接收的.
为什么不调用复制构造函数(即使通过引用执行异常处理)?
如果我想用vector中的值删除所有元素,我调用remove 算法,然后调用vector的erase成员函数来物理删除它.但是在list的情况下,简单的调用删除成员函数,它将删除具有该值的所有元素.我不确定为什么vector在列表执行时不提供删除MF.
对于Exp:我想从向量v中删除值"4".
vector<int> v;
vector<int> ::iterator Itr;
for (int i=0; i< 6; i++)
v.push_back(i*2);
v.push_back(4);
v.push_back(8);
v.push_back(4);
v.erase(remove(v.begin(),v.end(),4), v.end());
Run Code Online (Sandbox Code Playgroud)
和列表:
list.remove(4); // will delete all the element which has value 4
Run Code Online (Sandbox Code Playgroud) 我想之间的差异std::lock()
,并std::try_lock()
只在try_lock()
,如果锁不可用,立即就会在的情况下返回false时std::lock()
,它会阻塞状态去.
void lock( Lockable1& lock1, Lockable2& lock2, LockableN&... lockn );
Run Code Online (Sandbox Code Playgroud)
使用死锁避免算法锁定给定的Lockable对象lock1,lock2,...,lockn 以避免死锁.
int try_lock( Lockable1& lock1, Lockable2& lock2, LockableN&... lockn);
Run Code Online (Sandbox Code Playgroud)
尝试通过从第一个开始按顺序调用try_lock来锁定每个给定的Lockable对象lock1,lock2,...,lockn .
我有以下两个问题:
std::lock()
提供死锁避免但std::try_lock
不是?std::lock
,锁的顺序无关紧要(它可能是lock2,锁3,lock1,...),同时按std::try_lock()
顺序保持锁(lock1,lock2,lock3 ....)#include <iostream>
using namespace std;
class base1{};
class base2{virtual void show(){}};
class test1{ };
class test2{virtual void show(){}};
class derv1:public virtual base1{};
class derv12:public virtual base2{};
class derv2:public virtual base2, public test1{};
class derv22:public virtual base2, public virtual test1{};
class derv222:public virtual base2, public virtual test2{};
int main()
{
cout<<"sizeof base1 = "<<sizeof(base1)<<endl;
cout<<"sizeof base2 = "<<sizeof(base2)<<endl;
cout<<"sizeof derv1 = "<<sizeof(derv1)<<endl;
cout<<"sizeof derv12 = "<<sizeof(derv12)<<endl;
cout<<"sizeof derv2 = "<<sizeof(derv2)<<endl;
cout<<"sizeof derv22 = "<<sizeof(derv22)<<endl;
cout<<"sizeof derv222 = "<<sizeof(derv222)<<endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
sizeof …
Run Code Online (Sandbox Code Playgroud) 我调用shrink_to_fit()
了向量,它减少了向量中元素数量的容量,但是当我用于 shrink_to_fit()
字符串时,它减小了大小,但它不是字符串中元素的数量.
#include <iostream>
#include<vector>
#include<cstring>
using namespace std;
int main()
{
vector<char> v1;
v1.reserve(20);
v1.push_back('a');
v1.push_back('b');
cout << " vector capacity= "<<v1.capacity() <<endl;
v1.shrink_to_fit();
cout << " changed vector capacity= "<<v1.capacity() <<endl;
string s1;
s1.reserve(20);
s1.push_back('a');
s1.push_back('b');
cout <<" string capacity = " << s1.capacity() <<endl;
s1.shrink_to_fit();
cout <<" changed string capacity = " << s1.capacity() <<endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
vector capacity= 20
changed vector capacity= 2
string capacity = 30
changed string capacity = 15
Run Code Online (Sandbox Code Playgroud)