如何排序包含自定义(即用户定义)对象的向量.
可能应该使用标准STL算法与谓词(函数或函数对象)一起排序,该谓词将在自定义对象中的一个字段(作为排序键)上操作.
我是在正确的轨道上吗?
我有几个std::vector
,全长相同.我想对这些向量中的一个进行排序,并将相同的变换应用于所有其他向量.这样做有一个简洁的方法吗?(最好使用STL或Boost)?一些向量包含int
s,其中一些包含std::string
s.
伪代码:
std::vector<int> Index = { 3, 1, 2 };
std::vector<std::string> Values = { "Third", "First", "Second" };
Transformation = sort(Index);
Index is now { 1, 2, 3};
... magic happens as Transformation is applied to Values ...
Values are now { "First", "Second", "Third" };
Run Code Online (Sandbox Code Playgroud) 我有一个vector
的pair
像这样的:
vector<pair<string,double>> revenue;
Run Code Online (Sandbox Code Playgroud)
我想在地图中添加一个字符串和一个double,如下所示:
revenue[i].first = "string";
revenue[i].second = map[i].second;
Run Code Online (Sandbox Code Playgroud)
但由于收入未初始化,因此出现了出界错误.所以我尝试使用vector::push_back
这样的:
revenue.push_back("string",map[i].second);
Run Code Online (Sandbox Code Playgroud)
但那说不能采取两个论点.那么,如何可以添加到这个vector
的pair
?
你有一些有效的例程来返回数组中带有索引的数组吗?我认为使用stl向量存在一些方便的方法.你是否已经实现了一个没有stl的高效算法,或者你有一个伪代码或C++代码的参考?
感谢致敬
我这里的代码有两个数组.它对arr []进行排序,因此最高值将在索引0中.现在第二个数组arr1 []包含字符串,我希望代码将arr []所做的任何更改应用于arr1 [].因此arr [0]将返回6,而arr1 [0]将返回字符串"d1".请注意"d1"与6的索引是否相同?排序后我想要相同的值仍然有他们的字符串对应物.
我该怎么做呢?
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
int arr[ 5 ] = { 4, 1, 3, 6, 2 };
string arr1[ 5 ] = { "a1", "b1", "c1", "d1", "e1" };
std::sort( arr, arr + 5, std::greater< int >() );
cout << arr[0] << arr1[0] << endl; …
Run Code Online (Sandbox Code Playgroud) 这可能是最好的例子.我有两个向量/列表:
People = {Anne, Bob, Charlie, Douglas}
Ages = {23, 28, 25, 21}
Run Code Online (Sandbox Code Playgroud)
我想根据他们的年龄使用类似的东西来对人们进行排序sort(People.begin(), People.end(), CustomComparator)
,但我不知道如何编写CustomComparator
以查看Ages而不是People.
我得到的最好的例子是我想根据他们的分数对名字进行排序.
vector <string> Names {"Karl", "Martin", "Paul", "Jennie"};
vector <int> Score{45, 5, 14, 24};
Run Code Online (Sandbox Code Playgroud)
因此,如果我将分数排序为{5,14,24,45},则还应根据分数对名称进行排序.
我有这个两vector<double>
的mass
和velocity
两个大小相同的N
.它们包含有关N粒子质量和速度的信息.mass[i]
和velocity[i]
因而第i个粒子的属性
在C++中是否有可能将这两个向量"锁定"在一起并按质量递增顺序对它们进行排序?因此,在排序之后,矢量mass
应该以递增的顺序,并且速度矢量应该包含排序的质量的相应速度
例如,在排序质量=(4,2,1,3)和速度=(13,14,15,16)之后排序质量=(1,2,3,4)和速度=(15,14,16,13) )
我知道的一种(非高效)方式是将数据传输到struct的向量中
struct particle
{
double mass;
double velocity;
bool operator < (const particle& str) const
{
return (mass < str.mass);
}
};
Run Code Online (Sandbox Code Playgroud)
vector<particle> particlelist(N)
然后使用std::sort
by重载<
运算符创建然后对此向量进行排序,就像我在上面的定义中所做的那样.
我不想把我的数据放入Array of Structures时尚,因为我听说它与阵列结构方法相比效率低(至少在CUDA中).
我有一个"列"容器类型:
struct MyColumnType {
// Data: Each row represents a member of an object.
vector<double> a; // All vectors are guaranteed to have always
vector<string> b; // the same length.
vector<int> c;
void copy(int from_pos, int to_pos); // The column type provides an interface
void swap(int pos_a, int pos_b); // for copying, swapping, ...
void push_back(); // And for resizing the container.
void pop_back();
void insert(int pos);
void remove(int pos);
// The interface can be extended/modified if required
};
Run Code Online (Sandbox Code Playgroud)
用法: …
我通过名称(“红色”,“绿色”)创建颜色,但是稍后当我询问其名称时,就会获得RGB信息。是否有没有办法检索颜色名称(显然,它们不能全都有名称)。
#include <QColor>
#include <iostream>
int main( int argc, char* argv[] )
{
QColor color( "red" );
std::cout << color.name().toStdString();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出“#ff0000”,我希望输出“红色”。