我无法理解noexceptC++ 11/14中in关键字的用法和用途.我的意思是它是那些不发射的功能的签名exceptions.但它真的有效吗?
请看下面的代码:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void seev (vector<int> &v) noexcept;
void seev (vector<int> &v) noexcept
{
for (int i=0;i<10;++i)
{
cout<<v.at(i)<<' ';
}
}
int main()
{
vector<int> v {1,2,3,4,5};
seev(v);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码肯定会抛出一个out_of_range exception.所以noexcept这里的使用是没用的,或者是吗?
我的疑问是:
noexcept工作怎么样?
怎么用?
什么throw()不能做到这noexcept一点?
我遇到了问题,std::thread因为它不接受采用自动指定参数的函数.以下是一些示例代码:
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
void seev(const auto &v) // works fine with const vector<int> &v
{
for (auto x : v)
cout << x << ' ';
cout << "\n\n";
}
int main()
{
vector<int> v1 { 1, 2, 3, 4, 5 };
thread t(seev, v1);
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是编译器说:
[Error] no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, std::vector<int>&)'
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这是语言或GCC(4.9.2)的问题吗?
这次我和分配器一起尝试,并且觉得有很多机会泄漏资源.所以我想如果我std::unique_ptr以前处理它们会怎样.我试着用std::vector分配器试了一下手.我的代码是这样的: -
// allocator
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class X
{
int x,ID;
static int i;
public:
X()
{
cout<<"constructing ";
ID=++i;
cout<<"ID="<<ID<<'\n';
}
X(int a)
{
x=a;
cout<<"constructing ";
ID=++i;
cout<<"ID="<<ID<<'\n';
}
void get()
{
cout<<"enter x: ";
cin>>x;
}
void disp()
{
cout<<"x="<<x<<'\t';
}
~X()
{
cout<<"destroying ID="<<ID<<'\n';
}
};
int X:: i=0;
int main()
{
ios::sync_with_stdio(false);
vector<X> v;
auto alloc = v.get_allocator();
unsigned int i=0;
X *p(alloc.allocate(5));
for …Run Code Online (Sandbox Code Playgroud) 最近,我读了文字就足够像s,h,ms等在C++ 14已投入的命名空间std::literals.因此,如果我要使用它们,那么我应该包含命名空间或用于std::literals::表示这些后缀.然而,当我尝试以下程序(cpp.sh/9ytu)而不使用上述任何一项时,我得到了所需的输出: -
#include <iostream>
#include <thread>
using namespace std;
int main()
{
auto str = "He is there"s;
auto timegap = 1s;
cout << "The string is :-" << endl;
this_thread::sleep_for(timegap);
cout << str;
return 0;
}
/*Output:-
The string is :-
He is there
*/
Run Code Online (Sandbox Code Playgroud)
如你所见,我没有包括任何namespace或std::literals::仍然我的程序正确运行.我在Orwell DevC++,C++ Shell,Coliru中尝试了这个,并且到处都有相同的输出.有什么问题?
我type_traits最近一直在研究并且想知道为什么它们被实现,class templates而实现它们functions可能更明显并且具有更简单的语法.
我想说的是这种语法: -
int x = 5;
std::cout << is_same<int>(x);
Run Code Online (Sandbox Code Playgroud)
比实际的更有说服力和清洁,即: -
int x = 5;
std::cout << is_same <int, decltype(x)>::value;
Run Code Online (Sandbox Code Playgroud)
这只是好奇心的一个例子.我只想知道标准化委员会的理念,而class不是function方法.
最近我在C++中尝试了一个简单的异常程序,如下所示: -
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
int main()
{
int x=5, y=0;
try
{
int z=x/y;
cout<<"z="<<z;
}
catch (exception& e)
{
cout<<"exception caught: "<<e.what();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么这里没有例外?代码有什么问题?另外我想知道为什么&需要捕获异常?
我的代码是这样的: -
#include <iostream>
#include <thread>
using namespace std;
void swapno (int &a, int &b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int x=5, y=7;
cout << "x = " << x << "\ty = " << y << "\n";
thread t (swapno, x, y);
t.join();
cout << "x = " << x << "\ty = " << y << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码无法编译.任何人都可以帮我解释原因吗?不仅如此代码,但是代码这也无法发送std::unique_ptr的参考.怎么了std::thread?
最近我正在尝试使用一个程序,std::map并遇到了一种情况,我需要iterator在 switch case 中使用地图。我的程序是这样的:-
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
map<string,int> m;
//map<string,int>::iterator itr;
int q, mark, type;
string name;
cin >> q;
while (q--)
{
cin >> type;
switch (type)
{
case 1: cin >> name >> mark;
m[name] += mark;
break;
case 2: cin >> name; …Run Code Online (Sandbox Code Playgroud) 我将首先用C++发布我的测试程序:
#include <iostream>
using namespace std;
class X
{
int x;
public:
X()
{
cout<<"constructing\n";
x=0;
}
int& getx()
{
return x;
}
~X()
{
cout<<"destroying\n";
}
};
int main()
{
X* p=(X*)malloc(sizeof(X));
++p->getx();
p->getx()*=5;
cout<<p->getx();
free(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
5
Run Code Online (Sandbox Code Playgroud)
现在,在任何人抱怨为什么我在C++程序中使用malloc&之前free,我想重申它只是一个测试程序,即使使用operator new&也可以完成上述操作operator delete.但我的问题仍然是:
malloc or operator new我们如何访问类X的变量x?free & operator delete也不要破坏对象并执行纯粹的分配.如果我new使用operator delete or free而不是使用而创建一个对象会发生什么delete?我的对象是否仍然存在并且仍然可用吗?