F****n 发帖数: 3271 | 1 I think I mentioned using HashSet or commons collection.
Advantages of different collections:
HashSet: contains / remove / hot remove
ArrayList/Vector: iteration / index
LinkedList: hot remove (remove by iterator)
If you want to have both HashSet and ArrayList's strength, you have to
combine them. org.apache.commons.collections.* already implement those
collections. |
|
c*****t 发帖数: 1879 | 2 There are many subtle issues with HashSet. Just because you are
using the pre-defined classes which "contained" the problem and
hide it from the eye, it doesn't mean they don't exist. Hash
collision for instance is a major problem, and the associated
item removal is another. They aren't better than simple array /
vector.
Database, for example, shown hash indexing isn't necessary better
than b+tree indexing:
http://archives.postgresql.org/pgsql-general/2005-05/msg00376.php
and
http://www.postg |
|
c*****t 发帖数: 1879 | 3 I was just saying that your understanding that (hashset is good for
hot removal) is wrong. You brought up hashset first, not me. Obviously,
if there are multiple objects fit the filter, it is either not a
set or there are not advantages using the hashset.
All I was trying to point out in the previous posts were that if
you have to do sequential search anyways to remove multiple objects
array / vector are a good choice due to very low overhead. |
|
F****n 发帖数: 3271 | 4 其实用过C或者经常用数组的很容易倾向于好虫的方法
想想如果是数组,肯定要新开一个,删掉位移估计只内存不够时才会用
而VECTOR其实就是一个封装的数组。 |
|
c*****t 发帖数: 1879 | 5 I listed two possibilities in that sentence, hehe :)
1. same as the hashcode, in that case, there aren't many objects but one,
which isn't what LZ wanted.
2. different from hashcode. Then one has to do sequential scan since
the container itself doesn't provide the quick access. Thus there
are no advantages using the HashSet all. Might as well just use
array/vector.
amount of finesse." |
|
m******t 发帖数: 2416 | 6
Right, that's the worse scenario out of the two.
I thought the idea was to use a set to temporarily hold
the items to be removed. So when we call removeAll on the
original vector, each lookup in the set would cost constant
time ideally. |
|
m*p 发帖数: 1331 | 7 it's not that trivial
核心问题,如何swap vector里面的2个elements? |
|
m****r 发帖数: 6639 | 8 oh, vector.set(int, E). that would work. |
|
N***m 发帖数: 4460 | 9 can we use vector.set(index,element)?
At the first glance, I thought LZ was asking how to make an efficient algo
to enumerate all possibilites, not just swapping. |
|
m*p 发帖数: 1331 | 10 nice, that's the way out i think! how to write the code to swap elements in
vector then? |
|
f********f 发帖数: 290 | 11 我一般就是一个
using namespace std;就完事了
看很多人习惯于用一个,加一个:using std::vector; using std::cin;....
难不成效率高??
thanks a lot |
|
c**c 发帖数: 39 | 12 我运行R的时候遇到这样的errror message:
Error: cannot allocate vector of size 375000 Kb
可是我查我的memory size 是:
memory.limit(size = NA)
[1] 1610612736
这么大的memory,怎么还不够呢? |
|
c********x 发帖数: 84 | 13 Wrong, Vector has it onw copy function, if I rember correctly. |
|
s*****r 发帖数: 773 | 14 怎么看到一个说法是vector holds objects of various types.
难道每个元素可以不一样么? |
|
|
l***e 发帖数: 480 | 16 想删向量里后面的元素.
元素是一个结构sn,由一个字符串name和一个整数count构成。
向量vsn已排好序。因为remove删不掉。就想用pop_back删。
while( vsn.end().count < 2 ) vsn.pop_back();
//将向量里元素中count值小于2的元素,删除。
编译时,总抱错。
error: âclass __gnu_cxx::__normal_iterator
allocator > >â has no member named âcountâ
while( (vsn.end()).count < 2 ) vsn.pop_back();
while( *(vsn.end()).count < 2 ) vsn.pop_back();
while( (*(vsn.end())).count < 2 ) vsn.pop_back();
都不行。
vsn.pop_back();没错,单句执行没问题。
如何引用最后一个元素的成员变量。
或者还有 |
|
t****t 发帖数: 6806 | 17 while (!vsn.empty() && vsn.back().count<2) vsn.pop_back();
因为你不能保证vsn.back()总是存在, 所以你要先看它是不是空的.
如果你知道一个iterator a, 要从这个iterator一直删到最后, 可以写
vsn.erase(a, vsn.end());
std::remove不是删除元素用的, 是把要删的元素交换到range的最后. 删除用
container.erase(). 所以对于vector, 可以这样用:
vsn.erase(remove(vsn.begin(), vsn.end(), your_condition), vsn.end());
这些在effective STL里都有...STL虽然很容易用, 但是完全不看手册光凭函数名想当
然, 也是不行的. |
|
n**d 发帖数: 9764 | 18 Could any ppl explain the output from vector code below? |
|
n**d 发帖数: 9764 | 19 I may know the answer. vector has to resize the memory for each push and
then move the existing data to new location.
costly
causes
that |
|
t****t 发帖数: 6806 | 20 有指针的类, 99%需要定义copy constructor和op=
op=记得要检测self-assignment
vector的定义没问题的 |
|
b***y 发帖数: 2799 | 21 ☆─────────────────────────────────────☆
xubest (既飞之蜻蜓) 于 (Fri Mar 14 19:57:51 2008) 提到:
比如有一很大的文件,读的时候用定义好的数组来存,速度是不是要比挨个push_back
快很多?
☆─────────────────────────────────────☆
Xentar (思考猪) 于 (Fri Mar 14 20:15:33 2008) 提到:
如果你预先知道大小可以用reserve一次分配好这些空间。
back
☆─────────────────────────────────────☆
PaulPierce (Paul) 于 (Sat Mar 15 01:31:32 2008) 提到:
我用vector和自己定义的CLASS速度比是,20分钟:1分钟
我也不知道是不是我用的不好
back
☆─────────────────────────────────────☆
pptwo (pp) 于 (Sat Mar 15 01:36:10 2008) |
|
f**y 发帖数: 138 | 22 Never mind. The objects in Java Vector or Map are already references. |
|
P********e 发帖数: 2610 | 23 首先要有vector,然后才有他的iterator |
|
l**t 发帖数: 64 | 24 istream_iterator head(in);
istream_iterator tail;
vector temp;
copy(head, tail, back_inserter(temp)); |
|
i*****l 发帖数: 50 | 25 要把一个常数矩阵存在一个class里面
我知道可以把这个常数矩阵声明成static的
比如
class A
{
static const double Array[10][10];
}
const double Array={{.....},{....},{....}....}
但是因为矩阵太大乐,懒的一个个的输入,想用for循环(因为很多数剧是一样的)
应改怎么写呢?
3x, 用array和vector都无所谓 |
|
t****t 发帖数: 6806 | 26 他要全局的吧, 不能这么干的
in fact, before c++0x is out, there is almost not point declaring a
static const vector<...> xxx
since there is almost no way to initialize it.
如果很大的话, 还不如自动生成一个.h然后include 进来呢 |
|
q******u 发帖数: 46 | 27 对vector应该没有本质区别吧?其他container就另当别论。
有人说[]慢,不过看了看源码就是
return *(this->_M_impl._M_start + __n);
没想明白怎么慢了?
虽然是个土问题,但一直没有搞明白... |
|
t****t 发帖数: 6806 | 28 struct Person { string name; int age; };
vector person;
do |
|
t****t 发帖数: 6806 | 29 i guess you were a java user
in c++ you don't have to initialize v by "v=vector();", or do anything
to release it.
you also don't have to write myClass a=myClass(); just write myClass a; is
enough. |
|
|
t****t 发帖数: 6806 | 31 that has nothing to do with low efficiency of vector::push_back(), no? |
|
O*******d 发帖数: 20343 | 32 Vector内部是一块连续的内存。 当capacity用完后,就要把全部数据转到新的更大的
连续内存上。 |
|
d****p 发帖数: 685 | 33 Among all STL containers, vector is the fastest if number of elements is
less than a modest number, say 100. |
|
w*****j 发帖数: 49 | 34 Hi, everyone. I've been trying to use cin to read a sentence I typed in into
a vector of string . But it seems like it doesn't work. It only read in the
first word. Does anybody get any suggestion or tell me how cin deals with
strings? |
|
z****e 发帖数: 2024 | 35 or if you really want to get screen command line input as source, and you
want " " as delimiter.
string s1;
vector vstr;
getline(cin,s1);//command line input
size_t p1=0;
size_t p2=0;
while( (p2=s1.find_first_of(" ",p2)) != string::npos){
cout<
vstr.push_back(s1.substr(p1, p2-p1));
p2=s1.find_first_not_of(" ",p2);
p1=p2;
}
vstr.push_back(s1.substr(p1,p2-p1));//read the last string |
|
s*****g 发帖数: 5159 | 36 Solved by
vec.end() -------> vec.back()
vecto
vector
b |
|
z****e 发帖数: 2024 | 37 不需要delete a, ~A(){}时候,vector自动化解。
你这个每次都push一下,的确比较慢。
事先reserve()比较好。 |
|
h****8 发帖数: 599 | 38 因为foo1里面你又新定义并且初始化了一个变量_stack,不是原来那个成员变量
把vector去掉,改为 _stack(capacity);就行了 |
|
z****e 发帖数: 2024 | 39 a new local vector is defined in your ctor. |
|
k*******d 发帖数: 1340 | 40 Effective STL里面说过,最好别用vector
如果真的是3G的话,32bit的操作系统可能无法寻址到,虽然理论上能到4G |
|
z****e 发帖数: 2024 | 41 vector 要求要可以拷贝,你没有定义拷贝构造函数,而恰恰是因为这个错误,编译器
自动生成的浅拷贝,被析构的时候,delete了。然后再次被delete,是重复delete问题。
你要避免这个错误,就要写一个深拷贝的拷贝构造函数。 |
|
d****p 发帖数: 685 | 42 #include
std::vector tokens;
boost::split(tokens, "value1;value2;value3;value4", boost:is_any_of(";");
// now tokens[i] = "value"
a |
|
X****r 发帖数: 3557 | 43 reserve() does not change the size of the vector.
So x[0] = 5; results in undefined behavior even after
x.reserve(3) or x.reserve(10).
So |
|
c**********e 发帖数: 2007 | 44 You guys are all right. I tested and confirmed that reserve() only changes
capacity but not size. decamp explains well why the value of x[0] is kept if
push_back is used.
There is another related interview question. How to change the size of a
vector? (I can push_back many times, but it does not seem a good answer.) |
|
t****t 发帖数: 6806 | 45 if you want to erase every element this will be O(n^2) for vector. not good. |
|
g*********s 发帖数: 1782 | 46 but vector must keep the continuous storage semantics. |
|
S**I 发帖数: 15689 | 47 this will cause run-time error almost for sure; erase a vector iterator
invalidates all iterator to elements after the erased iterator. |
|
t****t 发帖数: 6806 | 48 for encapsulated containers (or other objects), people intend to "use" the
exposed methods, e.g. vector::erase(). if it is an array and there is no
erase() method, most likely they won't simulate it.
humans are lazy. if a human want to go through a wall, and someone give him
a pick, he will probably dig through the wall. but if there is no pick, he
probably will take the easy way -- walk around it. haha. |
|
r********3 发帖数: 2998 | 49 还有个办法,你的intro to algorithm里面的洗牌算法嘛?
如果我要删除中间的元素,我就先把这个元素和最后一个元素交换,然后删除最后一个
元素,这样就不用移动了。
当然,这样要是无序的vector才行。 |
|
t*****t 发帖数: 52 | 50 if processing order is not important, why use vector. the set and
unordered_set is much simpler in semantics, and, most important, easily
parallelized. |
|