我有几个std::vector,全长相同.我想对这些向量中的一个进行排序,并将相同的变换应用于所有其他向量.这样做有一个简洁的方法吗?(最好使用STL或Boost)?一些向量包含ints,其中一些包含std::strings.
伪代码:
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) 我有两个向量:
vector1 = [1 2 3 4 5 6 7 8 9]
vector2 = [1 2 3 4 5 6 7 8 9]
我想确保,当我使用random_shuffle进行随机播放时,它们应该以相同的顺序进行混洗.例如:
洗牌后的输出应该是:
vector1 = [1 9 3 4 2 7 8 5 6]
vector2 = [1 9 3 4 2 7 8 5 6]
但我得到的输出如下:
vector1 = [5 1 7 4 2 3 9 8 6]
vector2 = [3 4 1 9 8 2 5 7 6]
继承我的代码:
int main ()
{
std::srand ( unsigned …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序,用户可以在向量中输入任意数量的值,它应该返回四分位数,但我不断得到"向量下标超出范围"错误:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <ios>
#include <vector>
int main () {
using namespace std;
cout << "Enter a list of numbers: ";
vector<double> quantile;
double x;
//invariant: homework contains all the homework grades so far
while (cin >> x)
quantile.push_back(x);
//check that the student entered some homework grades
//typedef vector<double>::size_type vec_sz;
int size = quantile.size();
if (size == 0) {
cout << endl << "You must enter your numbers . "
"Please …Run Code Online (Sandbox Code Playgroud) 我有一个包含类的对象的std::vector调用.假设有一个成员变量,我还实现了一个返回最小值和的函数.然后,我可以根据对象的值来做矢量.foo_vecFooFooint xCompareInts(int a, int b)abstd::sortx
但是,如果这些x值不是成员变量Foo,而是在另一个std::vector名称中,那该怎么办呢x_vec?这里,第一个元素x_vec对应于第一个元素foo_vec,依此类推.如何根据相应的值执行std::sort打开?foo_vecx_vec
我有一个边数组,它被定义为C风格的双精度数组,其中每4个双精度定义一个边,如下所示:
double *p = ...;
printf("edge1: %lf %lf %lf %lf\n", p[0], p[1], p[2], p[3]);
printf("edge2: %lf %lf %lf %lf\n", p[4], p[5], p[6], p[7]);
Run Code Online (Sandbox Code Playgroud)
所以我想用std::sort()边长来对它进行排序.如果是struct Edge { double x1, y1, x2, y2; }; Edge *p;,我会很高兴.
但在这种情况下,double数组的块大小不是由指针类型表示的.qsort()允许您显式指定块大小,但通过指针类型std::sort() 推断块大小.
出于性能原因(内存使用和CPU),让我们说创建新数组或以某种方式转换数组是不可取的.出于性能原因,让我们说我们确实想要使用std::sort()而不是qsort().
是否可以std::sort()在转换数据时不浪费单个CPU周期进行调用?
一个明显的方法是尝试强制转换指针:
double *p = ...;
struct Edge { double arr[4]; };
Edge *p2 = reinterpret_cast<Edge*>(p);
std::sort(...);
Run Code Online (Sandbox Code Playgroud)
但是如何确保数据正确对齐?另外,如何确保它始终在所有平台和架构上正确对齐?
或者我可以使用typedef double[4] Edge;?
以前作为一个面试问题,并且在基本语法上窒息得太厉害,我没能推进(一旦肾上腺素开始,编码就会消失.)
给定一个字符串列表,返回一组字符串列表,这些字符串是输入集的字谜.即"狗","上帝","foo"应该返回{"dog","god"}.之后,我自己创建了代码作为一个完整性检查,它现在已经存在了一段时间.我欢迎对它进行输入,看看我是否遗漏了任何东西,或者我是否可以更有效地完成任务.把它当作改善自己和学习其他技巧的机会:
void Anagram::doWork(list input, list> &output)
{
typedef list < pair < string, string>> SortType;
SortType sortedInput;
// sort each string and pair it with the original
for(list< string >::iterator i = input.begin(); i != input.end(); ++i)
{
string tempString(*i);
std::sort(tempString.begin(), tempString.end());
sortedInput.push_back(make_pair(*i, tempString));
}
// Now step through the new sorted list
for(SortType::iterator i = sortedInput.begin(); i != sortedInput.end();)
{
set< string > newSet;
// Assume (hope) we have a match and pre-add the first.
newSet.insert(i->first);
// Set …Run Code Online (Sandbox Code Playgroud) 有一个10,000的数组.它以随机顺序存储数字1到10,000.
每个号码只出现一次.
现在,如果从该数组中删除任何数字,并将任何其他数字复制到数组中.
我们如何确定哪个数字是重复的,最小的复杂性?
注意:我们不能使用另一个阵列.
我需要对一大堆大型物体进行排序,这让我想到:有没有办法减少掉期数量?
所以我使用quicksort(但任何其他快速排序也应该在这里工作)来将索引排序到数组中的元素; 指数交易便宜.然后我使用这些索引将实际对象交换到它们的位置.不幸的是,这使用O(n)额外空间来存储索引.下面的代码说明了算法(我称之为IndexSort),在我的测试中,对于大型对象的数组,它似乎比plain quicksort快.
template <class Itr>
void IndexSort(Itr begin, Itr end)
{
const size_t count = end - begin;
// Create indices
vector<size_t> ind(count);
iota(ind.begin(), ind.end(), 0);
// Sort indices
sort(ind.begin(), ind.end(), [&begin] (const size_t i, const size_t j)
{
return begin[i] < begin[j];
});
// Create indices to indices. This provides
// constant time search in the next step.
vector<size_t> ind2(count);
for(size_t i = 0; i < count; ++i)
ind2[ind[i]] = i;
// …Run Code Online (Sandbox Code Playgroud)