我试图从派生类模板访问基类中的typedef成员.模板参数的模板名称与基类中的typdef名称相同.
#include <iostream>
using namespace std;
class no {
public :
typedef int T;
};
template<typename T> class no1 : public no {
public :
T obj;
};
int main() {
// your code goes here
no1<string> o ; o.obj = "1";
return 0;
}
14:24: error: invalid conversion from 'const char*' to 'no::T {aka int}' [-fpermissive]
no1<string> o ; o.obj = "1";
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,T是obj,总是int类型.如何强制obj是模板参数而不是基类中声明的typdef?
谢谢
有没有办法处理泛型catch块中抛出的异常.
try
{
throw ;
}
catch(...)
{
// how to get handle to exception thrown
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我知道数组提供对其任何元素的恒定时间访问,但是从开始或中间插入或移除元素需要移动后续元素,有时需要调整整个数组的大小和复制.这可以在最坏的情况下给出线性复杂性.
我们可以通过使用memcpy使数组中的随机插入更有效,如下例所示.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
// your code goes here
int arr[16] = {0};
for ( int i = 0 ; i < 10 ; i++ )
{
arr[i] = 1;
}
std::cout << std::endl << "INPUT ARR:";
for ( int i = 0 ; i < 16 ; i++ )
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
std::memcpy(arr+9,arr+8,sizeof(int)*2); arr[8] = 9;// std::cout << std::endl << "NO " << …Run Code Online (Sandbox Code Playgroud) 我刚刚注意到string :: find的这种奇怪行为.我有一个非空字符串b和另一个空字符串a.当我调用b.find(a)时,它应返回npos但返回0.
#include <iostream>
#include <string>
using namespace std;
int main() {
// your code goes here
string a , b("ABC");
if ( string::npos == b.find(a) ) std::cout << std::endl << "TRUE" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码打印不正确.有人可以解释一下这意味着什么吗?由于a是空的并且b是非空的,因此在非空中找到空字符串没有意义,因此错误.所以它应该返回npos
谢谢