由买买提看人间百态

topics

全部话题 - 话题: typename
1 2 3 4 5 下页 末页 (共5页)
d*******n
发帖数: 524
1
来自主题: Programming版 - 这两个地方是否需要typename?
对typename这个关键词的用法非常不理解,下面这段代码里面划下划线的地方是否需要
typename?为什么?谢谢
class NotEnoughElements {};
template
typename Container::value_type
Reduce(const Container& c, Function fn) throw (NotEnoughElements)
{
if(c.size() < 2)
throw NotEnoughElements();
______ Container::value_type result = *c.begin();
for(______ Container::iterator it = c.begin()+1; it < c.end(); it
++)
{
result = fn(result, *it);
}
d***q
发帖数: 1119
2
来自主题: Programming版 - 这两个地方是否需要typename?
when you want to use it as a type you must tell compiler it is a type.
the typename is used for that.
for example
class XX
{
public:
typedef int Int;
};
when you want to use Int to defines some variables, you probably will write
like: XX::Int xx; however compiler will complain XX::Int is not a type
instead a static variable. In order to inform compiler it is a type, put
typename ahead like: typename XX::Int xx;
d****b
发帖数: 25
3
来自主题: Programming版 - 一个关于关键字typename的问题
假设有一个模版tree class
template
{
private:
struct node
{
T elem;
node left;
node right;
}
node * head;
public:
typename tree::node * traversal( node * p, const T & e);
......................
};
这个traversal函数返回一个node 指针, 为什么前边必须要定义为 typename tree:
多谢!
h**i
发帖数: 57
4
C++ has two ways to specify a formal parameter for a template:
class identifier
typename identifier
are there any difference between thetwo notations? If so, what are they?
b********n
发帖数: 609
5
不全对。如果template parameter是一个template class,就一定要用class,
不能用typename。
c********e
发帖数: 383
6
never heard of this. and i definitely used typename in the case u
described.
t****t
发帖数: 6806
7
hmm, i am not sure on this
template class K> ...
maybe you can not use "typename K" here. but what i meant is, when they can
both be used then they are equivalent [14.1, clause 2]
p*u
发帖数: 2454
8
C++ templates: the complete guide page 51, K has to be declared by
keyword class, while T can be typename.

can
x*****n
发帖数: 3422
9
来自主题: Programming版 - C++ implicit typename的问题
I got it. Now we need:
typename set::const_iterator i;
d*******n
发帖数: 524
10
来自主题: Programming版 - 这两个地方是否需要typename?
我对这个typename的用法非常不清楚,大牛可不可以给科普一下什么时候该用什么时候
不该用?
或者给个链接讲这个问题讲的比较清楚的?
谢谢了
d***q
发帖数: 1119
11
来自主题: Programming版 - 这两个地方是否需要typename?
whey you want to use it as a type let compiler know it is a type.
for example
you define
class XX
{
public:
typedef in Int;
};
when you try to use the type to define some variables like:
XX::Int xx; compiler will consider that XX::Int is a static variable.
you must tell compiler XX::Int is a type so you need to write:
typename XX::Int xx;
X****r
发帖数: 3557
12
来自主题: Programming版 - 一个关于关键字typename的问题
对于tree::node来说,由于tree这个模版类还没有实例化,
编译器并不知道tree::node是一个变量还是一个类型,所以你需
要用typename来告诉编译器这是一个类型。而如果不是写tree::node
而是直接写node,编译器就自动查找到当前范围里的node这个名字,
知道这是个类型了。
r******r
发帖数: 700
13
我当年的 homework,翻出来了。另一个 homework project, 帮我找到了第一个工作。
#ifndef LIST_H
#define LIST_H
#include
#include
#include
#include "node.h"
using namespace std;
/*=========================================================================*/
/**
* Implementation of a List ADT using a doubly-linked list.
*
* @version 1.0 2/25/05
*/
/*.........................................................................*/
/**
* Definition of exception handling class
*/
class ListEmpty : public ... 阅读全帖
x******a
发帖数: 6336
14
//main()
#include
#include
#include "linkedlist.h"
int main()
{
LinkedList myList(5, "luke");
Iterator myIterator=myList.begin();
myList.insert(myIterator, "leia");
myIterator.forward();
myList.erase(myIterator);
myList.insert(myIterator, "chewbca"); 《======出问题。
myList[2]="han";
for (int i=0; i std::cout< }
myList.insert(myIterator, "chewbca"); 《======出问题。
这一句运行起来ta... 阅读全帖
r****t
发帖数: 10904
15
非 template 情况似乎比较清楚了,class template 里面的 static data
member 的定义和初始化有没有啥龟腚?
举个例子:
template
class StackWithCapacity : public stack_adapter
{
public:
typedef typename stack_adapter< Container>::size_type size_type;
private:
static const size_type capacity = 5;
};
capacity 是个 static const member, 我就直接在 class body 里面初始化了,
据 c++ primer,class body 外面还必须 define without initializer (虽然我我不这么做也能编译并运行正常). 这样我就在外面加
template
StackWithCapacity... 阅读全帖
t****t
发帖数: 6806
16
来自主题: JobHunting版 - 一道A家店面题求解
#include
#include
enum search_type { Exact, MaxLess, MinGreater, MaxLessEqual, MinGreaterEqual
};
/* returns "next" element if key was to be inserted. */
template
typename Container::const_iterator
search_place(const Container& array, typename Container::value_type key,
bool ascending)
{
if (ascending) {
return lower_bound(array.begin(), array.end(), key, std::less<
typename Container::value_type>());
} else {
return lower_bou... 阅读全帖
d**s
发帖数: 98
17
http://zhedahht.blog.163.com/blog/static/2541117420071289522817
程序员面试题精选100题(02)-设计包含min函数的栈[数据结构]
2007-02-28 21:52:28| 分类: 栈 | 标签:编程 就业 找工作 |字号大中小 订阅
题目:定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数
min、push以及pop的时间复杂度都是O(1)。
分析:这是去年google的一道面试题。
我看到这道题目时,第一反应就是每次push一个新元素时,将栈里所有逆序元素排序。
这样栈顶元素将是最小元素。但由于不能保证最后push进栈的元素最先出栈,这种思路
设计的数据结构已经不是一个栈了。
在栈里添加一个成员变量存放最小元素(或最小元素的位置)。每次push一个新元素进
栈的时候,如果该元素比当前的最小元素还要小,则更新最小元素。
乍一看这样思路挺好的。但仔细一想,该思路存在一个重要的问题:如果当前最小元素
被pop出去,如何才能得到下一个最小元素?
因此仅仅只添加一个成员变量存放最小元素(或最... 阅读全帖
x******a
发帖数: 6336
18
Thanks a lot! it worked.
再请教一个问题:我想历遍一个通过frontinsert生成的linkedlist,为什么看不到第
一个insert的元素?谢谢
#include
#include "singlylinkedlist.h"
int main(int argc, const char * argv[])
{
SingleLinkedList aList;
int i=0;
while(i<10){
aList.firstInsert(i);
++i;
}
aList.traverse();
}
输出是
9 8 7 6 5 4 3 2 1
没有0
template< typename T> class ListElement;
template< typename T> class SingleLinkedList;
template
class ListElement{
public:
ListElement(cons... 阅读全帖
f*********m
发帖数: 726
19
来自主题: JobHunting版 - 谷歌面经
第二题贴个template List版的,请各位指正:
template
class NextClass
{
public:
NextClass(list >& L):CList (L), listListIt(L.begin()), listIt((*L.
begin()).begin())
{
while (listlistIt != CList.end())
{
if ((*listlistIt).begin() != (*listlistIt).end())
{
listIt = (*listlistIt).begin();
break;
}
++listlistIt;
}
if ((*listlistIt).begin() == (*listlistIt).end())
c... 阅读全帖
h**********c
发帖数: 4120
20
think in cpp vol 2 chapter 3
//: C03:TemplateTemplate.cpp
#include
#include
#include
using namespace std;
// As long as things are simple,
// this approach works fine:
template
void print1(C& c) {
typename C::iterator it;
for(it = c.begin(); it != c.end(); it++)
cout << *it << " ";
cout << endl;
}
// Template-template argument must
// be a class; cannot use typename:
template class C>
void print2(C& c) {
copy(c.begin(), c
g****n
发帖数: 14
21
来自主题: Programming版 - C++ template question
You should write:
template
class test
{
public:
test() {}
~test() {}
template <> friend bool operator== (const test &p1, const
test &p2); //line 7
};
In this case, only a specialized instance of operator== is the friend of
test.
If you write
class test
{
public:
test() {}
~test() {}
template friend bool operator== (const test &
p1, const test &p2); //line 7
};
All the instances of template operator=
m*****m
发帖数: 242
22
来自主题: Programming版 - 问几个C++面试题吧
1.
template
bool is_int();
is_int must return true if T is int and false otherwise. How do you
implement is_int?
Choice 1
template
bool is_int()
{
return T is int;
}
Choice 2
template
bool is_int()
{
return false;
};
template
bool is_int()
{
return true;
};
Choice 3
template
bool is_int()
{
T a;
return dynamic_cast(&a) != 0;
}
Choice 4
template
bool is_int()
{
return typeof(T)
r*******m
发帖数: 109
23
来自主题: Programming版 - C++ template Questions (转载)
template
typename Container::value_type
Reduce(const Container& c, Function fn)
{
if (c.size()<1) throw NotEnoughElement();
typename Container::value_type T(0);
for (typename Container::const_iterator it=c.begin();it!=c.end();++it)
T=fn(T,*it);
return T;
}
h********n
发帖数: 1671
24
来自主题: Programming版 - C++中如何引用模板类中的模板函数
这个例子中,模板类B中引用了模板类A的模板函数f(),但是gcc无法识别这个用法。
template< typename T >
struct A
{
template< typename U >
int f (){ return 0; }
};
template< typename T >
struct B
{
A< T > a; // error
//A a; // ok
int g (){ return a.f< char >(); }
};
int main()
{
B< int > b;
return b.g();
}
U:\tmp\test.cpp: In member function 'int B::g()':
U:\tmp\test.cpp:15:31: error: expected primary-expression before 'char'
U:\tmp\test.cpp:15:31: error: expected ';' before ... 阅读全帖
t****t
发帖数: 6806
25
来自主题: JobHunting版 - 问个构造函数的问题
呃, 看到第一个回贴我以为他懂的
看到第二个回贴我才知道他是装懂的
这个答案是这样的
class B
{
public:
B(args_1);
B(args_2);
// and many constructors with different arg lists
};
class D : public B
{
public:
template D(T args_1) : B(args_1) {}
template D(T1 args_1, T2 args_2) : B(args_1, args
_2) {}
/* ...按参数个数写出D::D的template */
};
并不是把B变成模板.
当然, 如果允许C++11的话, 就直接在D里写using B::B; 就可以了.
不过, 这个特性目前gcc还没支持. 如果用支持的特性, 可以用variadic template (
supported in gcc 4.3+), 这样就只要写一个模板就可以了, 不用按个数:
template 阅读全帖
t****t
发帖数: 6806
26
来自主题: Programming版 - template question
MyVector::Iterator is a dependent type, so use the keyword "typename"
template
typename MyVector::Iterator MyVector::begin()
// same for end
// <<
{
typename MyVector::Iterator iter;
....
B********e
发帖数: 1062
27
来自主题: Programming版 - C++ question about template typedef
template
struct MapofVec{
typedef vector VecType;
typedef typename vector::iterator VecIter;
typedef map MapType;
typedef typename map::iterator MapIter;
};

use
**
w******g
发帖数: 67
28
来自主题: Programming版 - C++ question about template typedef
Thanks a lot. Another question: if I want to define a function template to
print out the elements of the MapOfVec.
template
struct MapOfVec
{
typedef vector VecType;
typedef typename vector::iterator VecIter;
typedef map MapType;
typedef typename map::iterator MapIter;
};
template
void printElems_MapOfVec(const MapOfVec::MapType& map);
It a
p*u
发帖数: 2454
29
来自主题: Programming版 - c++ 设计问题求助

anywhere by
setter/
you are right, but we can always solve this with another level of
indirection:
"
#define GETTER( a ) Intruder::get( *this, a )
#define SETTER( a, i ) Intruder::set( *this, a, i )
class A
{
private:
friend struct Intruder;
int _i;
};
struct Intruder
{
template
static int get( const T& ref, const A& a );
template
static void set( const T& ref, A& a, int i );
};
template
int Intruder::get( const T& ref, const A& a )... 阅读全帖
b**a
发帖数: 1375
30
下面这个function在visual studio 2010下没问题
template
typename T::value_type reduce(const T& t, Fn f)
{
T::value_type result = *t.begin();
T::const_iterator it=t.begin();
for (++it; it!=t.end(); ++it)
result = f(result, *it);
return result;
}
调用的话比如:
vectorv;
v.push_back(1); v.push_back(2); v.push_back(3);
cout << reduce(v, plus())< 但是在linux的gcc(4.1 -> 4.6)下出现好几个类似
error: need ‘typename’ before ‘T:: value_type’ becau... 阅读全帖
j******n
发帖数: 271
31
来自主题: Programming版 - 讨论 找单链表倒数m的节点 (转载)
What is the concern here? In each iteration, at most 4 pointers are
referenced: two pointers to the current and the two pointer to next elements
of them. Even if they are all in different pages, in each iteration we
reference at most 4 pages, which is 4*4KB and can fit in today's L1 cache.
Below is my solution without advancing each pointer twice. Please comment.
1: #include
2: #include
3: #include
4: using namespace std;
5:
6: template阅读全帖
j*****u
发帖数: 186
32
下面程序来自于essential C++ 3.6节,给定一个数组,输出比某个数小的所以元素。
main函数中用了back_inserter,按道理是不是应该include iterator头文件和
using std::back_inserter? 但是为什么把这两句注释掉程序也能跑呢?谢谢。
===================================================================
#include
#include
#include
#include
#include
//#include
using std::vector;
using std::cout;
using std::endl;
using std::less;
//using std::back_inserter;
template 阅读全帖
j*****u
发帖数: 186
33
下面程序来自于essential C++ 3.6节,给定一个数组,输出比某个数小的所以元素。
main函数中用了back_inserter,按道理是不是应该include iterator头文件和
using std::back_inserter? 但是为什么把这两句注释掉程序也能跑呢?谢谢。
===================================================================
#include
#include
#include
#include
#include
//#include
using std::vector;
using std::cout;
using std::endl;
using std::less;
//using std::back_inserter;
template 阅读全帖
t****t
发帖数: 6806
34
template
typename StackWithCapacity::size_type StackWithCapacity::capacity;

太乌龙了,可是
template
StackWithCapacity::size_type StackWithCapacity::
capacity;
or
template
size_type StackWithCapacity::capacity;
也出一模一样的错误,这个我实在是搞不懂。。
g****y
发帖数: 436
35
来自主题: Programming版 - C++ template function type
请问下面MAPPER的type应该如何写?先谢过了!
//hist_helper.h
namespace hist_aux {
template<
typename C,
typename T,
typename MAPPER
>
void getHist(const T& vals,
MAPPER pf,
C& res) {

typename T::const_iterator it;
for (it = vals.begin(); it != vals.end(); ++it) {
res.at(pf(*it))++;
}
}
} /* namespace hist_aux */
namespace hist{
class Hist {
public:
// ctr & dtr
// ...

size_t count_mapper(size_t v);
size_t index_mapper(size_t v);
size_t pixe... 阅读全帖
X****r
发帖数: 3557
36
来自主题: Programming版 - C++ template function type
First, you can't deduce a template parameter from return type.
Second, returning a vector by value is neither efficient nor flexible,
e.g.
what if you want to accumulate histogram?
Third, you don't have to specify vector as input or output.
So what you want is probably something like the following:
template
void histo(const V& vals, const MAPPER& pf, C& result) {
typename V::const_iterator it;
for (it = vals.begin(); it != vals.end(); ++it) {
resu... 阅读全帖
T********i
发帖数: 2416
37
来自主题: Programming版 - 建议大家看看interlocked
#if defined(_MSC_VER)
# pragma once
#include
inline long long InterlockedIncrement(long long volatile *ptr) {
return InterlockedIncrement64(ptr);
}
inline long long InterlockedDecrement(long long volatile *ptr) {
return InterlockedDecrement64(ptr);
}
#else
#include
template
inline T InterlockedIncrement(T volatile *ptr) {
return __sync_add_and_fetch(ptr, 1);
}
template
inline T InterlockedDecrement(T volatile *ptr) {
return __sync... 阅读全帖

发帖数: 1
38
来自主题: Programming版 - 请教高手一个C++问题
如果是C++11或以上的话,可以加一段std::hash>的specialization:
------------------------------------------
namespace std {
template
struct hash> {
template
inline static std::size_t CombineHashes(const TupleT &arg,
std::enable_if_t* = 0) {
std::size_t first_hash = CombineHashes(arg);
std::size_t second_hash = std::hash<
typename std::tuple_element阅读全帖
l******o
发帖数: 144
39
来自主题: JobHunting版 - 问一道精华帖的老题
随便写了一个,懒得去测试了。
#include
#include
#include
#include
#include
using namespace std;
template
struct Segment
{
T left;
T right;
};
template
struct LeftEndComp
{
bool operator () (const Segment& one, const Segment& two) const
{
return one.left < two.left || (!(two.left < one.left) && one.right <
two.right);
}
};
template
T coverage(const vector >& segments)
{
if(se
l********n
发帖数: 54
40
来自主题: JobHunting版 - 一道STL面试题
我的理解是这样的:
简单的说,这是由于C++的编译方式和模板的特性导致的。
一个例子:test.cpp, template.cpp, template.h
=====================================
template.h
template
class Temp
{
private:
T _val;
public:
void setVal(T& val);
T& getVal();
};
=====================================
=====================================
template.cpp
#include "template.h"
template
void Temp::setVal(T& val)
{
_val = val;
}
template
T& Temp::getVal()
{
return _val;
n*******r
发帖数: 22
41
来自主题: JobHunting版 - C++ Singleton Template - 编译通不过
在Visual Studio上编译总是有错,哪位大侠能不能帮我看看是什么原因?
error LNK2019: unresolved external symbol "private: __thiscall
Singleton2::Singleton2(void)" (??0?$Singleton2@H@@AAE@XZ)
referenced in function "public: static class Singleton2 * __cdecl
Singleton2::GetInstance(void)" (?
GetInstance@$Singleton2@H@@SAPAV1@XZ)
// Singleton.h
template class Singleton2
{
private:
Singleton2(void);
~Singleton2(void);
Singleton2(const Singleton2&);
Singleton2& operator=(const Si... 阅读全帖
q*******p
发帖数: 39
42
来自主题: JobHunting版 - C++ template Questions
有1道题请教大家
1. 实现 Reduce function using templates. The Reduce fn applies a function
of two arguments cumulatively to the items of an STL container, from
begin() to end(), so as to reduce the sequence to a single value. For
example, Reduce(, std::plus()) should
calculate ((((1+2)+3)+4)+5).
class NotEnoughElements {};
template
typename Container::value_type
Reduce(const Container& c, Function fn) throw (NotEnoughElements)
{
实... 阅读全帖
b**********2
发帖数: 1923
43
来自主题: JobHunting版 - 帮我看看这两个题目回答
2. Your task is to implement the Reduce function using
templates. The Reduce fn applies a function of two arguments
cumulatively to the items of an STL container, from begin() to end(),
so as to reduce the sequence to a single value. For example,
Reduce(, std::plus()) should
calculate ((((1+2)+3)+4)+5).
class NotEnoughElements : public exception {
public:
virtual const char* what() const throw() {
return “Not enough elements in the
container... 阅读全帖
i***0
发帖数: 8469
44
来自主题: JobHunting版 - 大家来讨论一下c++吧
大家来讨论一下c++吧
Please answer the following c++ questions. Do not simply answer yes/no.
Always explain your assumptions and reasoning.
1. memset is sometimes used to initialize data in a constructor like the
example below. What is the benefit of initializing this way? Does it work
in this example? Does it work in general ? Is it a good idea in general?
class A {
public:
A();
private:
int a;
float f;
char str[35];
long *lp;
};
A::A()
{
... 阅读全帖
a***e
发帖数: 1140
45
来自主题: JobHunting版 - 问个构造函数的问题
template
class B
{
public:
B(T arg) {}
};
template<>
class B
{
public:
B(T1 arg) {}
};
...
template
class D : public B
{
public:
D(T arg) : B(arg) {}
};
class D does not need to change.
suppose class B has other new c'tors, it just need to add specification
template class to refine it.
g*******s
发帖数: 2963
46
来自主题: JobHunting版 - 面完G的电面了,忐忑
这个行不?constructor里我pass了container(这里是vector),因为我不知道没有
container该怎么写hasNext()? 如果假设原始iterator已经有hasNext()和next()了,
那这俩函数应该不用改吧?看成black box就行了,直接在peek里引入个temp变量就行
了。
#include
#include
using namespace std;
template
class Wrapper {
public:
Wrapper(vector &v,
typename vector::iterator it)
:_vec(v),_it(it),_tempIt(it){
_it = _vec.begin();
};
bool hasNext(){
_tempIt = _it;
return (++_tempIt)!=_vec.end();
};
... 阅读全帖
r*******e
发帖数: 7583
47
来自主题: JobHunting版 - 问一道C++ template的面试题
template
class IteratorWrapper {
public:
IteratorWrapper(T& container)
: d_container(container), d_iter(container.begin()) {
}
bool hasNext() {
return d_iter != d_container.end();
}
typename T::reference next() {
typename T::reference v = *d_iter;
++d_iter;
return v;
}
private:
T& d_container;
typename T::iterator d_iter;
};

iterator赋
h**o
发帖数: 548
48
来自主题: JobHunting版 - 包子求大牛:C++的list iterator实现
谁考你了?
template
class Node{ //used by List
Node(T data, Node* next):_data(data), _next(next){}
T _data;
Node* _next;
friend class List; //only friend can call private
friend class ListIterator;
};
template
class List{
List(Node* head=NULL, Node* tail=NULL);
List(const List& orig);
List& operator=(const List& orig);
push_front();push_end();pop_front();pop_end();
//the following are used for iterator:
typedef ListIterator... 阅读全帖
i***0
发帖数: 8469
49
来自主题: Seattle版 - 大家来讨论一下c++吧
大家来讨论一下c++吧
Please answer the following c++ questions. Do not simply answer yes/no.
Always explain your assumptions and reasoning.
1. memset is sometimes used to initialize data in a constructor like the
example below. What is the benefit of initializing this way? Does it work
in this example? Does it work in general ? Is it a good idea in general?
class A {
public:
A();
private:
int a;
float f;
char str[35];
long *lp;
};
A::A()
{
... 阅读全帖
d****n
发帖数: 1637
50
In your search function, you need to use the typename keyword to give
the compiler the extra hint that BinarySearchTree::tree_node is a
type. Node in your class class is still an incomplete type with template
type in. In VC it may work without typename.(not tested)
template
typename BinarySearchTree::tree_node*
BinarySearchTree::search(tree_node* p, T d)
{
if(p==NULL||d==p->data)
return p;
else if(d>p->data)
return search(p->right,d);
else
re... 阅读全帖
1 2 3 4 5 下页 末页 (共5页)