我需要在std :: vector中找到一个元素位置,用它来引用另一个向量中的元素:
int find( const vector<type>& where, int searchParameter )
{
for( int i = 0; i < where.size(); i++ ) {
if( conditionMet( where[i], searchParameter ) ) {
return i;
}
}
return -1;
}
// caller:
const int position = find( firstVector, parameter );
if( position != -1 ) {
doAction( secondVector[position] );
}
Run Code Online (Sandbox Code Playgroud)
但是vector::size()返回size_t对应于unsigned不能直接存储的整数类型-1.当使用size_t而不是int作为索引时,如何表示在向量中找不到元素?
哪能push_back一struct成矢量?
struct point {
int x;
int y;
};
std::vector<point> a;
a.push_back( ??? );
Run Code Online (Sandbox Code Playgroud) 使用向量,可以假设元素连续存储在内存中,允许范围[&vec [0],&vec [vec.capacity())用作普通数组.例如,
vector<char> buf;
buf.reserve(N);
int M = read(fd, &buf[0], N);
Run Code Online (Sandbox Code Playgroud)
但现在向量不知道它包含M个字节的数据,由read()外部添加.我知道vector :: resize()设置了大小,但它也清除了数据,因此在read()调用之后它不能用于更新大小.
是否有一种简单的方法可以将数据直接读入矢量并在之后更新大小?是的,我知道明显的解决方法,比如使用一个小数组作为临时读缓冲区,并使用vector :: insert()将它附加到向量的末尾:
char tmp[N];
int M = read(fd, tmp, N);
buf.insert(buf.end(), tmp, tmp + M)
Run Code Online (Sandbox Code Playgroud)
这是有效的(这就是我今天正在做的事情),但是如果我可以将数据直接放入向量中,那么在那里会有一个额外的复制操作,这是困扰我的.
那么,在外部添加数据时,是否有一种简单的方法来修改矢量大小?
所有,
这个问题的延续这一个.我认为STL错过了这个功能,但它只是我的恕我直言.
现在,问题.
考虑以下代码:
class Foo
{
public:
Foo();
int paramA, paramB;
std::string name;
};
struct Sorter
{
bool operator()(const Foo &foo1, const Foo &foo2) const
{
switch( paramSorter )
{
case 1:
return foo1.paramA < foo2.paramA;
case 2:
return foo1.paramB < foo2.paramB;
default:
return foo1.name < foo2.name;
}
}
int paramSorter;
};
int main()
{
std::vector<Foo> foo;
Sorter sorter;
sorter.paramSorter = 0;
// fill the vector
std::sort( foo.begin(), foo.end(), sorter );
}
Run Code Online (Sandbox Code Playgroud)
在任何给定的时刻,矢量都可以重新排序.该类还具有在分类器结构中使用的getter方法.
在向量中插入新元素的最有效方法是什么?
我的情况是:
我有一个网格(电子表格),它使用类的排序向量.在任何给定时间,可以重新排序向量,并且网格将相应地显示排序的数据.
现在我需要在向量/网格中插入一个新元素.我可以插入,然后重新排序然后重新显示整个网格,但这对于大网格来说效率非常低. …
我有一个包含很少非相邻重复项的向量.
举个简单的例子,考虑一下:
2 1 6 1 4 6 2 1 1
Run Code Online (Sandbox Code Playgroud)
我试图vector通过删除不相邻的重复项并保持元素的顺序来使这个独特.
结果将是:
2 1 6 4
Run Code Online (Sandbox Code Playgroud)
我尝试的解决方案是:
手动重复消除:
Define a temporary vector TempVector.
for (each element in a vector)
{
if (the element does not exists in TempVector)
{
add to TempVector;
}
}
swap orginial vector with TempVector.
Run Code Online (Sandbox Code Playgroud)我的问题是:
是否有任何STL算法可以从向量中删除不相邻的重复项?它的复杂性是什么?
我是一名编程学生,对于我正在研究的项目,我必须做的事情是计算int值向量的中值.我这样做只使用排序功能从STL和矢量成员函数,如.begin(),.end()和.size().
我也应该确保我找到矢量具有奇数个值或偶数个值的中位数.
我被困了,下面我已经把我的尝试包括在内了.那我哪里错了?如果您愿意给我一些指导或资源以便朝着正确的方向前进,我将不胜感激.
码:
int CalcMHWScore(const vector<int>& hWScores)
{
const int DIVISOR = 2;
double median;
sort(hWScores.begin(), hWScores.end());
if ((hWScores.size() % DIVISOR) == 0)
{
median = ((hWScores.begin() + hWScores.size()) + (hWScores.begin() + (hWScores.size() + 1))) / DIVISOR);
}
else
{
median = ((hWScores.begin() + hWScores.size()) / DIVISOR)
}
return median;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!!
在以下代码中:
std::vector<int> var;
for (int i = 0; i < var.size(); i++);
Run Code Online (Sandbox Code Playgroud)
size()成员函数是为每个循环迭代调用的,还是仅调用一次?
我刚刚阅读了这篇博客http://lemire.me/blog/archives/2012/06/20/do-not-waste-time-with-stl-vectors/,比较了operator[]作业的性能和push_back预留的内存std::vector,我决定亲自尝试一下 操作很简单:
// for vector
bigarray.reserve(N);
// START TIME TRACK
for(int k = 0; k < N; ++k)
// for operator[]:
// bigarray[k] = k;
// for push_back
bigarray.push_back(k);
// END TIME TRACK
// do some dummy operations to prevent compiler optimize
long sum = accumulate(begin(bigarray), end(array),0 0);
Run Code Online (Sandbox Code Playgroud)
这是结果:
~/t/benchmark> icc 1.cpp -O3 -std=c++11
~/t/benchmark> ./a.out
[ 1.cpp: 52] 0.789123s --> C++ new
[ 1.cpp: 52] 0.774049s --> C++ new
[ 1.cpp: …Run Code Online (Sandbox Code Playgroud) 如何在我的矢量文件中添加paddingleft,Right,Top和bottom veriable.
我变了
机器人:viewportWidth
和
机器人:viewportHeight
但没有什么变化
我的矢量
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="31.416dp"
android:height="31.416dp"
android:viewportWidth="31.416"
android:viewportHeight="31.416">
<path
android:fillColor="#fff"
android:pathData="M28.755,6.968l-0.47,0.149L25.782,7.34l-0.707,1.129l-0.513-0.163L22.57,6.51l-0.289-0.934L21.894,4.58l-1.252-1.123
l-1.477-0.289l-0.034,0.676l1.447,1.412l0.708,0.834L20.49,6.506l-0.648-0.191L18.871,5.91l0.033-0.783l-1.274-0.524l-0.423,1.841
l-1.284,0.291l0.127,1.027l1.673,0.322l0.289-1.641l1.381,0.204l0.642,0.376h1.03l0.705,1.412l1.869,1.896l-0.137,0.737
l-1.507-0.192l-2.604,1.315l-1.875,2.249l-0.244,0.996h-0.673l-1.254-0.578l-1.218,0.578l0.303,1.285l0.53-0.611l0.932-0.029
l-0.065,1.154l0.772,0.226l0.771,0.866l1.259-0.354l1.438,0.227l1.67,0.449l0.834,0.098l1.414,1.605l2.729,1.605l-1.765,3.372
l-1.863,0.866l-0.707,1.927l-2.696,1.8l-0.287,1.038c6.892-1.66,12.019-7.851,12.019-15.253
C31.413,12.474,30.433,9.465,28.755,6.968z" />
<path
android:fillColor="#fff"
android:pathData="M17.515,23.917l-1.144-2.121l1.05-2.188l-1.05-0.314l-1.179-1.184l-2.612-0.586l-0.867-1.814v1.077h-0.382l-2.251-3.052
v-2.507L7.43,8.545L4.81,9.012H3.045L2.157,8.43L3.29,7.532L2.16,7.793c-1.362,2.326-2.156,5.025-2.156,7.916
c0,8.673,7.031,15.707,15.705,15.707c0.668,0,1.323-0.059,1.971-0.137l-0.164-1.903c0,0,0.721-2.826,0.721-2.922
C18.236,26.357,17.515,23.917,17.515,23.917z" />
<path
android:fillColor="#fff"
android:pathData="M5.84,5.065l2.79-0.389l1.286-0.705l1.447,0.417l2.312-0.128l0.792-1.245l1.155,0.19l2.805-0.263L19.2,2.09l1.09-0.728
l1.542,0.232l0.562-0.085C20.363,0.553,18.103,0,15.708,0C10.833,0,6.474,2.222,3.596,5.711h0.008L5.84,5.065z
M16.372,1.562
l1.604-0.883l1.03,0.595l-1.491,1.135l-1.424,0.143l-0.641-0.416L16.372,1.562z
M11.621,1.691l0.708,0.295l0.927-0.295
l0.505,0.875l-2.14,0.562l-1.029-0.602C10.591,2.526,11.598,1.878,11.621,1.691z" />
</vector>
Run Code Online (Sandbox Code Playgroud)
像那样
可能吗?