g*********s 发帖数: 1782 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: gandjmitbbs (Nothing), 信区: JobHunting
标 题: 一道C++ STL面试题
发信站: BBS 未名空间站 (Sat Dec 15 20:42:09 2007)
问以下代码如何提高效率。
vector x;
for (int i=0; i<1000; i++){
x.push_back(i);
}
改成这个?
x.resize(1000);
for (int i=0; i<1000; i++){
x[i]=i;
} |
|
g*********s 发帖数: 1782 | 2 手头书不少,几本经典的都有了。但是看来看去还是对STL里的概念很不清楚,觉得这
些描述太抽象。谁能简明扼要地总结一下啊?
比如iterator,我的理解是推广的指针,是指向其他对象的对象。那么指针上的所有操
作iterator都支持吗?另外iterator又被分成input iterator, output iterator等等
。他们和iterator class之间是继承关系吗?input iterator和output iterator号称
只支持一小部分指针操作。另外书上说使用output iterator的算法必须是单遍算法,
这个又是什么意思?
下面这个例子将vs填上5个"hello"。但是为啥要用这个output iterator back_
inserter?直接vs.end()或者vs.last()不行吗?或者直接循环5次push_back。引入这
么细分的算法意义何在呢?
std::vector vs;
std::fill_n(std::back_inserter(vs), 5, "hello"); |
|
T********r 发帖数: 6210 | 3 Supposedly I want to delete multiple entries in a STL hash map. The code
looks like the following:
for (it = hash_table.begin(); it != hash_table.end(); it ++)
{
if (some_condition)
hash_table.erase (it);
}
My questions is, after erasing an entry referenced by 'it', will 'it' still
be valid such that you can use 'it++' to find the next entry in the hash map
? Or will 'it' automatically points to the next entry in the hash map
because the total number of entries already reduced by one |
|
c***d 发帖数: 996 | 4 ☆─────────────────────────────────────☆
fightwater (QT) 于 (Sun Sep 17 18:34:38 2006) 提到:
i wrote a program which uses the stl compose1 functor
but the compiler seems cannot find it
here is the code
//////////////////////////////////////////
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int n=10;
vector aa(n);
iota(aa.begin(),aa.end(), |
|
l***e 发帖数: 480 | 5 effective STL, do you e-doc to share with me? thanks. |
|
|
i***i 发帖数: 4 | 7 use 'pair' and define your own LessThan operator, then plug in STL sort
routine. |
|
r*********r 发帖数: 3195 | 8 working on a project that uses only C.
just wondering if there is any free/open C library
similar to STL (/w containers), in terms of code quality and popularity? |
|
d***q 发帖数: 1119 | 9 c doesn't need STL at all..hehe...
use void* instead |
|
r*********r 发帖数: 3195 | 10 a lot of them are declaration for the standard C library and
system calls. also, the STL algorithm functions will be included too.
that's why compiling c++ is so much slower than compiling c. |
|
r********d 发帖数: 23 | 11 就是阶乘呗
X<5>::v = 5*X<4>::v = 5*4*X<3>::v = ... = 120*X<0>::v = 120
ps. this is a question about template, not about STL. |
|
r*********r 发帖数: 3195 | 12 找本写得好的书看就不犯困了. 不过不能保证不上买买提.
STL 要想了解内部结构的话, 拿 SGI 版的源码读一读就够了, 很清楚.
大部分书啰哩啰唆讲半天讲不清楚, 一看源码就明白. |
|
b***y 发帖数: 2799 | 13 ☆─────────────────────────────────────☆
dArtagnan (达达尼昂) 于 (Thu Mar 13 10:47:37 2008) 提到:
从数据结构上来讲,对于一个链表,应该很容易实现扫描该链表的操作,只要从头开始
扫描链表的第一个元素,然后扫描该元素指向的那个元素,然后。。。。。。
但是在STL的list里面我似乎没有找到什么直接的方式可以扫描这个链表。
想问一下大家如果要扫描一个list一般是怎么做的?
难道用pop_front把第一个元素拿出来,扫描完了再给push_back回去?
或者我就完全用错了class,应该用一个其他的class?
☆─────────────────────────────────────☆
phennia (Phe) 于 (Thu Mar 13 10:50:45 2008) 提到:
iterator
☆─────────────────────────────────────☆
dArtagnan (达达尼昂) 于 (Thu Mar 13 10:53:49 2008) 提到:
T |
|
E*****7 发帖数: 128 | 14 请大家推荐一本学习C++ STL的好书, 谢谢! |
|
b***y 发帖数: 2799 | 15 ☆─────────────────────────────────────☆
zodiac (King of Nuts) 于 (Fri Sep 16 14:43:15 2005) 提到:
VC6 和VC.Net 2003debugger都不能显示vector list等类重载的[]operator,所以看不到
这些STL container里的内容。怎么解决呢?
☆─────────────────────────────────────☆
cuixue (我是勤劳勇敢聪明熊) 于 (Fri Sep 16 15:42:19 2005) 提到:
gdb can display it
到
☆─────────────────────────────────────☆
zodiac (King of Nuts) 于 (Fri Sep 16 15:59:15 2005) 提到:
windows下有什么?
☆─────────────────────────────────────☆
cuixue (我是勤劳勇敢聪明熊) 于 (Fri Sep 16 16 |
|
I*******e 发帖数: 1879 | 16 ☆─────────────────────────────────────☆
JungleMonkey (MonkeyInTheJungle) 于 (Mon May 25 11:46:11 2009) 提到:
能否在STL insert元素的时候就得到指向该元素的指针?以后就可以keep track of 该
元素?
非常感谢。
my_list.push_back(my_element);
my_map[key]=my_element;
☆─────────────────────────────────────☆
JungleMonkey (MonkeyInTheJungle) 于 (Mon May 25 11:53:23 2009) 提到:
My problem is that actually I have maps of lists, I want to be able to
keep track of the address of the very element that has been inserted
last...
☆───────────── |
|
e****d 发帖数: 333 | 17 i guess you mean effective STL right?item 47. |
|
S*********g 发帖数: 5298 | 18 yes, effective STL, not C++ |
|
m*****u 发帖数: 1342 | 19 Sample code below. My problem is after I insert first element, from
second time on, ticker_map->end() == map_iterator is always false, in
other words, it always find new element already existing in the map
(which is not the case).
I have dealt with STL map before, this is a new problem to me. Any
advice will be appreciated !!!
======
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
typedef map stock |
|
H*M 发帖数: 1268 | 20 yeah sorry my mistake
it is in cstdlib
can I conculde that all STL functions are fine with functors, and some are f
ine with function pointers at the same time? |
|
z***e 发帖数: 5393 | 21 almost right.
c++ programming language上说stl function只是指望你给的那个能够执行 operator(
) (...)的运算,不管是function pointer还是functor.
f |
|
d**a 发帖数: 84 | 22 Q1
David R. Musser and Atul Saini, Stl Tutorial & Reference Guide: C++
Programming With the Standard Template Library (Addison-Wesley Pub (Sd),
1996).
Q2
这个自己看src code就好了,gcc带的那个或者visuall c++的都行。 |
|
k****r 发帖数: 176 | 23 The C++ Standard Library: A Tutorial and Reference
Nicolai M. Josuttis
STL源码剖析
侯捷 |
|
a********r 发帖数: 58 | 24 What's the underlying sort algorithm for SET in C++ STL? Thanks! |
|
T*******x 发帖数: 8565 | 25 c++中 member type 就是为了stl设计的吧?看起来挺头大的。
比如vector::iterator。
为什么不设计成这样:iterator>,
这样是不是就可以免去了member type?
typename这样的keyword也可以不要了。
而且template有了统一简单的格式:
template
class A {
};
就是最基本的C++ class定义,除了多几个参数类型。
这样设计有什么问题吗? |
|
d*******n 发帖数: 524 | 26 The output of the following piece of simple code to test stl map is very
weird.
//-------------------code-----------------------------
#include
#include |
|
d****p 发帖数: 685 | 27 STL include both libs and sources which are built into your object files. So
you cannot remove all of them.
If you compile under release mode, some of these soruce codes will be
inlined and there will be no debug info. However, that means you cannot
debug. |
|
h***r 发帖数: 726 | 28 那有没有办法在调试时不进入stl source code?
So |
|
d****p 发帖数: 685 | 29 So that happens when you step into your function? Guess that's because the
parameters of the function involve STL code. In that case just set break
points inside your function.
A GUI frondend helps in this case: try DDD. If you are on great Mac however,
Xcode will set everything for you.
So |
|
h***r 发帖数: 726 | 30 unfornately, setting break points and using ddd are not good solution for me:
1. Getting out from stl is *usually* faster than setting the break points (
which invols getting to the function, setting break points and possible
disable it later on)
2. I am working remotely 99% time. I do not use ddd.
I still believe this can be solved.
however, |
|
|
|
g*********s 发帖数: 1782 | 33 I have some code using stl container and algorithm to debug. Below is what I get:
tag is a vector with size 3, the content is 0, 1, 2.
(gdb) p tag.begin()
$3 = {_M_current = 0x8053008}
(gdb) p tag.end()
$4 = {_M_current = 0x8053014}
(gdb) p tag.size()
$5 = 3
(gdb) p (tag.end() - tag.begin())
Attempt to take address of value not located in memory.
Why "3" is not properly output?
(gdb) p std::find(tag.begin(), tag.end(), 2)
No symbol "find" in namespace "std". |
|
g*********s 发帖数: 1782 | 34 So it seems I have to take the following weird detour?
std::list dummy_queue;
std::list::iterator dummy_it (dummy_queue.end());
X.resize(x_sz, dummy_it);
What if dummy_queue is changed later? That means the value in X is stale and potentially dangerous?
Any better solution? We don't have such headache if we just use self-defined doubly linked list and the initial value NULL for X elements. I expect a stl based solution can achieve similar behaviour. |
|
p*u 发帖数: 2454 | 35
and potentially dangerous?
defined doubly linked list and the initial value NULL for X elements. I
expect a stl based solution can achieve similar behaviour.
first question is why u have to use X, not the list directly? do u want
random access dummy_queue? u can add a X::iterator member to dummy queue
elements; when u delete from dummy_queue, u need to delete the element in
X first through X::iterator( note delete in vector is not very efficient
). but it's convoluted, why do u have to use X? |
|
g*****1 发帖数: 998 | 36 【 以下文字转载自 JobHunting 讨论区 】
发信人: guagua1 (), 信区: JobHunting
标 题: vc++的debug状态下如何watch stl map里的全部内容?
发信站: BBS 未名空间站 (Fri Mar 4 15:15:30 2011, 美东)
比如我有一个很大的map,但是watch里只能看前100,不知道如何才能看到全部? |
|
S****z 发帖数: 666 | 37 我不信STL这么智能
放的不是对象而是对象的指针呢? |
|
J*****n 发帖数: 4859 | 38 今天一个同事跟我说他的困惑
他有一个三维数组存成vector of vector of vector(assume i,j,k)
然后他对其中的k,进行排序,然后使用lower_bound function去查找相关的iter,重
复了2million time。
结果他发现这个的速度很慢,还不如他自己写的binary search。(慢了一倍)
我想了半天,也没想出个名堂。
照理说stl的那些基本container和function都是优化的很好的,如果要慢,也不会慢一
倍那么多。
求高手指点一二,谢谢。 |
|
p***o 发帖数: 1252 | 39 Sounds like you are using the checked iterators.
Read your STL manual/source code to see if that could be the case.
Also you can compare asm listings to determine why. |
|
b******n 发帖数: 592 | 40 C++ Standard Library..印刷很好,读起来不累。STL,和IO都覆盖得很全 |
|
r*********r 发帖数: 3195 | 41 stl, iostream 直接看看源码就明白了.
除了源码, 其他的都是二手资料, 有可能越看越糊涂. |
|
r*******y 发帖数: 1081 | 42 Is this map a kind of hash map?
STL map will keep the key in order, but a general hash map does not require
this, right?
Thanks |
|
r*******y 发帖数: 1081 | 43 thanks.
So STL does not provide hashing?
usually
for |
|
w****o 发帖数: 2260 | 44 【 以下文字转载自 JobHunting 讨论区 】
发信人: winhao (勇敢的人), 信区: JobHunting
标 题: 问一下STL里的queue, and stack 遍历的问题
发信站: BBS 未名空间站 (Sun Mar 4 02:36:31 2012, 美东)
好像std里queue 和stack的实现没有提供iterator,怎么遍历呢?就是说怎么查看queue
,stack里的elements?
感觉没法弄。比如queue,除非把queue给弄散了,一个一个的 front(), pop(),来查看
里面的东西,可是这样的话,queue就给破坏了。
stack也有同样的问题。
谁给说说有什么好的方法?难道要自己新创造一个实现?
谢谢! |
|
w****o 发帖数: 2260 | 45 谢谢!
1. 其实吧,在看别人的代码,想加点自己的代码,试些东西,他们用了queue,我想在
某个特定的情况下,想看一下到底queue放了些什么东西,或者是想看看里面有没有满
足某些条件的元素。
2. 你说的对,vector, deque都有iterator,如果要方便的话,就用这些好了。
还有,我得出了结论,stl里的container并不是每个都有iterator的。
谢谢啦!
you
or |
|
z*****k 发帖数: 600 | 46 实在不懂C++,但是应急帮着写 VC C++ code。
几个问题:
1。
hash_map hmap(less(), hmp1_Alloc);
知道这是个是用stl的定义hash_map object, hmap(less(), hmp1_Alloc)是初始
化吗?那个 less()里的“()”是什么意思?
2。
以下也是个定义“less”为什么没有“()”。
hash_map > > hmap;
3。
哪里有VC C++ Hash map 的sample code?
谢谢。 |
|
z*****k 发帖数: 600 | 47 实在不懂C++,但是应急帮着写 VC C++ code。
几个问题:
1。
hash_map hmap(less(), hmp1_Alloc);
知道这是个是用stl的定义hash_map object, hmap(less(), hmp1_Alloc)是初始
化吗?那个 less()里的“()”是什么意思?
2。
以下也是个定义“less”为什么没有“()”。
hash_map > > hmap;
3。
哪里有VC C++ Hash map 的sample code?
谢谢。 |
|
s******s 发帖数: 505 | 48 C++ STL list 的实现:
为什么先写一个base类型,然后再从base继承出主要类型,比如list?
这个base似乎在任何别的地方都用不到。
这样做,从编程方法上有什么优越之处?谢谢! |
|
s******s 发帖数: 505 | 49
The header file noted that the purpose was to "split into" a
non-template base and a template type node. However, I fail
to see the point of doing this, since the STL list is only used and
known as a template class. |
|
s******s 发帖数: 505 | 50
+
Now I am confused. Sorry I don't know C++ well.
I thought STL was part of libstdc++. |
|