- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想做什么
我有一种划分事物的方法。此方法不会对数组进行完全排序;它只是简单地对数组进行分区,以便一侧的所有元素(某些预先确定的“中心”或“中点值”——但它不必导致均匀拆分)小于“中心”和另一侧的所有元素都大于中心。 Point:它不是传统意义上的“排序”;这是一个分区。
当我分区东西时,我需要保留一把 key ;以便在交换事物时,交换 key ;如果将来某个时候我想撤消分区,我可以根据 key 重新排列内容。
显然,要根据键值重新排列事物,我可以执行以下操作
std::vector< std::pair< std::size_t , my::thingie > > vp;
std::vector< std::size_t >::iterator itKey( key.begin() );
// itThingie_begin and itThingie_end exist; I don't have direct access to the container
my::thingie::iterator itThingie( itThingie_begin );
for (; itKey != key.end(); ++itKey; ++itThingie ) vp.push_back( *itKey, *itThingie );
std::sort( vp.begin() , vp.end() , &comp_pair_first );
itThingie = itThingie_begin;
for ( std::vector< std::pair< std::size_t , my::thingie > >::const_iterator p=vp.begin(); p!=vp.end(); ++p, ++itThingie ) *itThingie = p->second;
#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
#include <iterator>
//
// pair of iterators
//
template <typename T,typename U>
struct piter : public std::pair<T,U>
{
piter() : std::pair<T,U>() {};
piter( T const & l , U const & r ) : std::pair<T,U>(l,r) {};
piter( std::pair<T,U> const & p ) { this->first = p.first; this->second = p.second; };
//piter( std::pair<T,U> const p ) { this->first = p.first; this->second = p.second; };
template <typename OT, typename OU>
piter( piter<OT,OU> const & p ) : std::pair<T,U>::first(p.first), std::pair<T,U>::second(p.second) {}
piter<T,U> & operator=( piter<T,U> const & rhs )
{
if( &rhs != this ) { *this->first = *rhs.first; *this->second = *rhs.second; }
return *this;
};
friend void swap( piter<T,U> & lhs, piter<T,U> & rhs )
{
using std::swap;
std::cout << "piter::swap; WAS: " << *lhs.first << " <-> " << *rhs.first << std::endl;
std::iter_swap(lhs.first,rhs.first);
std::iter_swap(lhs.second,rhs.second);
std::cout << "piter::swap; NOW: " << *lhs.first << " <-> " << *rhs.first << std::endl;
};
};
//
// iterator of pair of iterators
//
template <typename T, typename U>
class piterator : public std::iterator< std::random_access_iterator_tag,
piter<T,U>,
std::ptrdiff_t,
piter<T,U> *,
piter<T,U> & >
{
typedef piterator<T,U> iter;
public: // Traits typedefs, which make this class usable with algorithms which need a random access iterator.
typedef std::random_access_iterator_tag iterator_category;
typedef piter<T,U> value_type;
typedef std::ptrdiff_t difference_type;
typedef piter<T,U> * pointer;
typedef piter<T,U> & reference;
public:
piterator() {};
piterator( iter const & rhs ) { this->mp.first = rhs.mp.first; this->mp.second = rhs.mp.second;};
piterator( pointer rhs ) { this->mp.first = rhs->first; this->mp.second = rhs->second; };
//piterator( reference const rhs ) { this->mp.first = rhs.first; this->mp.second = rhs.second; };
piterator( value_type const rhs ) { this->mp.first = rhs.first; this->mp.second = rhs.second; };
iter & operator=( iter const & rhs )
{
if ( &rhs != this ){ this->mp.first = rhs.mp.first; this->mp.second = rhs.mp.second; };
return *this;
}
friend void swap( iter & lhs , iter & rhs )
{
using std::swap;
std::cout << "piterator::swap; WAS: lhs " << *lhs->first << " rhs " << *rhs->first << std::endl;
swap(lhs.mp,rhs.mp);
std::cout << "piterator::swap; NOW: lhs " << *lhs->first << " rhs " << *rhs->first << std::endl;
}
public: // Comparison
// Note: it's an error to compare iterators over different files.
bool operator< ( iter const & rhs ) const { return mp.first < rhs.mp.first; }
bool operator> ( iter const & rhs ) const { return mp.first > rhs.mp.first; }
bool operator==( iter const & rhs ) const { return mp.first == rhs.mp.first; }
bool operator!=( iter const & rhs ) const { return mp.first != rhs.mp.first; }
public: // Iteration
iter & operator++() { ++mp.first; ++mp.second; return *this; }
iter & operator--() { --mp.first; --mp.second; return *this; }
iter operator++(int) { iter tmp(*this); ++(*this); return tmp; }
iter operator--(int) { iter tmp(*this); --(*this); return tmp; }
public: // Step
iter & operator+=( difference_type n ) { mp.first += n; mp.second += n; return *this; }
iter & operator-=( difference_type n ) { mp.first -= n; mp.second -= n; return *this; }
iter operator+ ( difference_type n ) { iter result(*this); return result += n; }
iter operator- ( difference_type n ) { iter result(*this); return result -= n; }
public: // Distance
difference_type operator-( iter & rhs ) { return mp.first - rhs.mp.first; }
public: // Access
reference operator*() { return mp; }
reference operator[]( difference_type n ) { return *(*this+n); }
pointer operator->() { return ∓ };
private: // State
value_type mp;
};
template<class T,class U>
bool proxy_comp( piter<T,U> left, piter<T,U> right )
{
std::cout << "proxy_comp: " << *(left.first) << " > " << *(right.first) << " ?=? " << ( *(left.first) > *(right.first) ) << std::endl;
return *left.first > *right.first;
}
int main()
{
std::vector<double> dv(3);
std::vector<int> iv(3);
dv[0] = -0.5; dv[1] = -1.5; dv[2] = -2.5;
iv[0] = 10; iv[1] = 20; iv[2] = 3;
typedef piterator< std::vector<int>::iterator , std::vector<double>::iterator > PAIR_ITER;
typedef PAIR_ITER::value_type PAIR_REF;
PAIR_ITER pair_begin( PAIR_REF( iv.begin() , dv.begin() ) );
PAIR_ITER pair_end( PAIR_REF( iv.end() , dv.end() ) );
std::cout << "paired arrays now:" << std::endl;
for ( PAIR_ITER p = pair_begin; p != pair_end; ++p )
std::cout << *p->first << " " << *p->second << std::endl;
std::cout << "swap 1st and 3rd elements..." << std::endl;
swap(*pair_begin,*(pair_begin+2));
std::cout << "paired arrays now:" << std::endl;
for ( PAIR_ITER p = pair_begin; p != pair_end; ++p )
std::cout << *p->first << " " << *p->second << std::endl;
std::cout << "calling sort..." << std::endl;
std::sort( pair_begin , pair_end , &proxy_comp<std::vector<int>::iterator , std::vector<double>::iterator> );
std::cout << "paired arrays now:" << std::endl;
for ( PAIR_ITER p = pair_begin; p != pair_end; ++p )
std::cout << *p->first << " " << *p->second << std::endl;
return 0;
}
paired arrays now:
10 -0.5
20 -1.5
3 -2.5
swap 1st and 3rd elements...
piter::swap; WAS: 10 <-> 3
piter::swap; NOW: 3 <-> 10
paired arrays now:
3 -2.5
20 -1.5
10 -0.5
calling sort...
proxy_comp: 20 > 3 ?=? 1
proxy_comp: 10 > 3 ?=? 1
paired arrays now:
3 -2.5
3 -2.5
3 -2.5
最佳答案
行。问题与 STL 如何移动内存有关。
它一直使用 swap() ,然后一切都会好起来的,但有时确实如此
(来自 gnu 的 STL_algo.h __insertion_sort)
if (__comp(*__i, *__first))
{
// COPY VALUE INTO TEMPORARY MEMORY
typename iterator_traits<_RandomAccessIterator>::value_type __val = _GLIBCXX_MOVE(*__i);
// MOVE MEMORY AROUND
_GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
// COPY TEMPORARY VALUE BACK
*__first = _GLIBCXX_MOVE(__val);
}
value_type operator*() const { return helper_class_value_collection_ctor( _args_ ); };
*piterator_a = *piterator_b
reference operator*() { return private_reftype_variable; };
#include <vector>
#include <iostream>
#include <utility>
#include <iterator>
#include <algorithm>
// forward decl
template <typename T,typename U> struct piterator_iterators;
template <typename T,typename U>
struct piterator_values
{
// This class holds memory; it is a value_type
// It only serves the purpose of
// allowing the stl to hold temporary values when moving memory around.
// If the stl called sort(), then this class wouldn't be necessary.
//
// Note that the memory may be set by a piterator_iterators class,
// which is a pseudo-value_type that points at memory, instead of holding memory.
//
// YOU NEED THIS SO THAT
// typename piterator<T,U>::value_type Tmp = *piterator_a
// PLACES THE VALUES INTO SOME (ACTUAL) TEMPORARY MEMORY, AS OPPOSED
// TO CREATING A NEW POINTER TO EXISTING MEMORY.
typedef typename T::value_type first_value;
typedef typename U::value_type second_value;
first_value first;
second_value second;
piterator_values() {};
piterator_values( first_value const & first , second_value const & second ) : first(first), second(second) {};
piterator_values( piterator_values<T,U> const & rhs ) : first(rhs.first), second(rhs.second) { };
piterator_values( piterator_iterators<T,U> const & rhs ) : first(*rhs.first), second(*rhs.second) { };
piterator_values<T,U> & operator=( piterator_values<T,U> const & rhs )
{
if( &rhs != this )
{
first = rhs.first;
second = rhs.second;
}
return *this;
};
piterator_values<T,U> & operator=( piterator_iterators<T,U> const & rhs )
{
if( &rhs != this )
{
first = *rhs.first;
second = *rhs.second;
}
return *this;
};
friend void swap( piterator_values<T,U> & lhs, piterator_values<T,U> & rhs )
{
using std::swap;
swap(lhs.first,rhs.first);
swap(lhs.second,rhs.second);
};
};
template <typename T,typename U>
struct piterator_iterators
{
T first;
U second;
// This class does not hold memory; it points at existing memory.
// It is a pseudo-value_type. When the piterator dereferences, it
// will return a piterator_iterators object IF it is a nonconst reference.
// This class is used as a "reference" for an actual iterator,
// so assignment operators change the value of the thing pointed at,
// as opposed to reseting the address of what is being pointed at.
//
// YOU NEED THIS SO THAT
// *piterator_a = *piterator_b
// MAKES SENSE.
// IF THE DEREFERENCE PASSED A piterator_values,
// THEN IT WOULD ONLY MODIFY A TEMPORARY, NOT THE ACTUAL THING
//
piterator_iterators() {};
piterator_iterators( T const & first , U const & second ) : first(first), second(second) {};
piterator_iterators( piterator_iterators<T,U> const & rhs ) : first(rhs.first), second(rhs.second) {};
piterator_iterators<T,U> & operator=( piterator_iterators<T,U> const & rhs )
{
if( &rhs != this )
{
*first = *rhs.first;
*second = *rhs.second;
}
return *this;
};
piterator_iterators<T,U> & operator=( piterator_values<T,U> const & rhs )
{
*first = rhs.first;
*second = rhs.second;
return *this;
};
friend void swap( piterator_iterators<T,U> & lhs, piterator_iterators<T,U> & rhs )
{
using std::swap;
std::iter_swap(lhs.first,rhs.first);
std::iter_swap(lhs.second,rhs.second);
};
};
//
// iterator of pair of iterators
//
template <typename T, typename U>
class piterator : public std::iterator< std::random_access_iterator_tag, piterator_values<T,U>, std::ptrdiff_t, piterator_iterators<T,U> *, piterator_iterators<T,U> & >
{
typedef piterator<T,U> iter;
public:
typedef std::random_access_iterator_tag iterator_catagory;
typedef typename piterator<T,U>::value_type value_type;
typedef typename piterator<T,U>::difference_type difference_type;
typedef typename piterator<T,U>::pointer pointer;
typedef typename piterator<T,U>::reference reference;
typedef piterator_iterators<T,U> value_of_reference;
//typedef typename piterator_iterators<T,U> & reference;
public:
piterator() {};
piterator( iter const & rhs ) { mp.first = rhs.mp.first; mp.second = rhs.mp.second; };
piterator( value_of_reference const rhs ) { mp.first = rhs.first; mp.second = rhs.second; };
piterator( T const first, U const second ) { mp.first = first; mp.second = second; };
iter & operator=( iter const & rhs )
{
if ( &rhs != this )
{
mp.first = rhs.mp.first;
mp.second = rhs.mp.second;
};
return *this;
}
friend void swap( iter & lhs , iter & rhs )
{
using std::swap;
swap(lhs.mp,rhs.mp);
}
public: // Comparison
bool operator< ( iter const & rhs ) const { return mp.first < rhs.mp.first; }
bool operator> ( iter const & rhs ) const { return mp.first > rhs.mp.first; }
bool operator==( iter const & rhs ) const { return mp.first == rhs.mp.first; }
bool operator!=( iter const & rhs ) const { return mp.first != rhs.mp.first; }
public: // Iteration
iter & operator++() { ++(mp.first); ++(mp.second); return *this; }
iter & operator--() { --(mp.first); --(mp.second); return *this; }
iter operator++(int) { iter tmp(*this); ++(*this); return tmp; }
iter operator--(int) { iter tmp(*this); --(*this); return tmp; }
public: // Step
iter & operator+=( difference_type n ) { mp.first += n; mp.second += n; return *this; }
iter & operator-=( difference_type n ) { mp.first -= n; mp.second -= n; return *this; }
iter operator+ ( difference_type n ) { iter result(*this); return result += n; }
iter operator- ( difference_type n ) { iter result(*this); return result -= n; }
difference_type operator+ ( iter const & rhs ) { return mp.first + rhs.mp.first; }
difference_type operator- ( iter const & rhs ) { return mp.first - rhs.mp.first; }
public: // Distance
difference_type operator-( iter & rhs ) { return mp.first - rhs.mp.first; }
public: // Access
// reference if on the lhs of the eq.
reference operator*() { return mp; }
// value if on the rhs of the eq.
value_type operator*() const { return value_type(*mp.first,*mp.second); }
reference operator[]( difference_type n ) { return *( (*this) + n ); }
pointer operator->() { return ∓ };
private: // State
value_of_reference mp;
};
////////////////////////////////////////////////////////////////
template<class T,class U>
bool proxy_comp( piterator_values<T,U> left, piterator_values<T,U> right )
{
return left.first < right.first;
}
///////////////////////////////////////////////////////////////
int main()
{
std::vector<double> dv1(3);
std::vector<double> dv2(3);
std::vector<int> iv(3);
dv1[0] = -0.5; dv1[1] = -1.5; dv1[2] = -2.5;
dv2[0] = 10.5; dv2[1] = 11.5; dv2[2] = 12.5;
iv[0] = 10; iv[1] = 20; iv[2] = 3;
//
// EXAMPLE 1: PAIR OF ITERATORS
//
typedef piterator< std::vector<int>::iterator , std::vector<double>::iterator > PAIR_ITER;
PAIR_ITER pair_begin( iv.begin() , dv1.begin() );
PAIR_ITER pair_end( iv.end() , dv1.end() );
std::cout << "paired arrays now:" << std::endl;
for ( PAIR_ITER p = pair_begin; p != pair_end; ++p )
std::cout << *p->first << " " << *p->second << std::endl;
std::cout << "swap 1st and 3rd elements..." << std::endl;
swap(*pair_begin,*(pair_begin+2));
std::cout << "paired arrays now:" << std::endl;
for ( PAIR_ITER p = pair_begin; p != pair_end; ++p )
std::cout << *p->first << " " << *p->second << std::endl;
std::cout << "calling sort..." << std::endl;
std::sort( pair_begin , pair_end , &proxy_comp<std::vector<int>::iterator , std::vector<double>::iterator> );
std::cout << "paired arrays now:" << std::endl;
for ( PAIR_ITER p = pair_begin; p != pair_end; ++p )
std::cout << *p->first << " " << *p->second << std::endl;
//
// EXAMPLE 2: TRIPLET (PAIR OF PAIR)
//
typedef piterator< std::vector<double>::iterator , std::vector<double>::iterator > DOUBLET_ITER;
typedef piterator< std::vector<int>::iterator , DOUBLET_ITER > TRIPLET_ITER;
TRIPLET_ITER triplet_begin( iv.begin(), DOUBLET_ITER( dv1.begin() , dv2.begin() ) );
TRIPLET_ITER triplet_end( iv.end(), DOUBLET_ITER( dv1.end() , dv2.end() ) );
std::cout << "tripleted arrays now:" << std::endl;
for ( TRIPLET_ITER p = triplet_begin; p != triplet_end; ++p )
std::cout << *p->first << " "
<< *p->second->first << " "
<< *p->second->second << std::endl;
std::cout << "iter_swap 1st and second elements..." << std::endl;
std::iter_swap( triplet_begin , triplet_begin+1 );
std::cout << "tripleted arrays now:" << std::endl;
for ( TRIPLET_ITER p = triplet_begin; p != triplet_end; ++p )
std::cout << *p->first << " "
<< *p->second->first << " "
<< *p->second->second << std::endl;
std::cout << "calling sort..." << std::endl;
std::sort( triplet_begin, triplet_end, &proxy_comp< std::vector<int>::iterator , piterator< std::vector<double>::iterator , std::vector<double>::iterator > > );
std::cout << "tripleted arrays now:" << std::endl;
for ( TRIPLET_ITER p = triplet_begin; p != triplet_end; ++p )
std::cout << *p->first << " "
<< *p->second->first << " "
<< *p->second->second << std::endl;
return 0;
}
paired arrays now:
10 -0.5
20 -1.5
3 -2.5
swap 1st and 3rd elements...
paired arrays now:
3 -2.5
20 -1.5
10 -0.5
calling sort...
paired arrays now:
3 -2.5
10 -0.5
20 -1.5
tripleted arrays now:
3 -2.5 10.5
10 -0.5 11.5
20 -1.5 12.5
iter_swap 1st and second elements...
tripleted arrays now:
10 -0.5 11.5
3 -2.5 10.5
20 -1.5 12.5
calling sort...
tripleted arrays now:
3 -2.5 10.5
10 -0.5 11.5
20 -1.5 12.5
关于符合 c++ STL 的迭代器迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6551076/
如果您有超过 1 个具有相同类名的(动态)文本框,并使用 jquery 循环遍历每个所述文本框,您是否可以假设每次选择文本框的顺序都是相同的? 示例: 文本框 1 值 = 1文本框 2 值 = 2文本
有人知道为什么这段代码无法顺利运行吗?它似乎不喜欢使用yield关键字进行迭代:我正在尝试从任何级别的列表或字典中挖掘所有数字(对列表特别感兴趣)。在第二次迭代中,它找到 [2,3] 但无法依次打印
我关于从 mysql 数据库导出数据并将其保存到 Excel 文件(多表)的创建脚本。我需要让细胞动态基因化。该脚本正确地显示了标题,但数据集为空。当我“回显”$value 变量时,我检查了数据是否存
我正在尝试在 Python 中运行模拟,由此我绘制了一个数组的随机游走图,给定了两个变量参数的设定水平。 但是,我遇到了一个问题,我不确定如何迭代以便生成 250 个不同的随机数以插入公式。例如我已经
我是学习 jquery 的新手,所以如果这是一个相对简单的问题,我深表歉意。我有一个 ID 为 ChartstoDisplay 的 asp.net 复选框列表。我正在尝试创建 jquery 来根据是否
我正在尝试根据在任意数量的部分中所做的选择找出生成有效案例列表的最佳方法。也许它不是真正的算法,而只是关于如何有效迭代的建议,但对我来说这似乎是一个算法问题。如果我错了,请纠正我。实现实际上是在 Ja
如果我使用 sr1 为 www.google.com 发送 DNSQR,我会收到几个 DNSRR(s) 作为回复,例如(使用 ans[DNSRR].show() 完成): ###[ DNS Resou
假设有这样一个实体类 @Entity public class User { ... public Collection followers; ... } 假设用户有成千上万的用户关注者。我想分页..
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Nested jQuery.each() - continue/break 这是我的代码: var steps =
我刚从 F# 开始,我想遍历字典,获取键和值。 所以在 C# 中,我会说: IDictionary resultSet = test.GetResults; foreach (DictionaryEn
我知道已经有很多关于如何迭代 ifstream 的答案,但没有一个真正帮助我找到解决方案。 我的问题是:我有一个包含多行数据的txt文件。 txt 文件的第一行告诉我其余数据是如何组成的。例如这是我的
我有 12 个情态动词。我想将每个模态的 .modal__content 高度与 viewport 高度 进行比较,并且如果特定模态 .modal__content 高度 vh addClass("c
在此JSFiddle (问题代码被注释掉)第一次单击空单元格会在隐藏输入中设置一个值,并将单元格的背景颜色设置为绿色。单击第二个空表格单元格会设置另一个隐藏输入的值,并将第二个单元格的背景颜色更改为红
这是一个非常具体的问题,我似乎找不到任何特别有帮助的内容。我有一个单链表(不是一个实现的链表,这是我能找到的全部),其中节点存储一个 Student 对象。每个 Student 对象都有变量,尽管我在
有没有办法迭代 IHTMLElementCollection? 比如 var e : IHTMLLinkElement; elementCollection:IHTMLElementCollect
我正在尝试用 Java 取得高分。基本上我想要一个 HashMap 来保存 double 值(因此索引从最高的 double 值开始,这样我更容易对高分进行排序),然后第二个值将是客户端对象,如下所示
我想在宏函数中运行 while/until 循环,并限制其最大迭代次数。我找到了如何在“通常”sas 中执行此操作: data dataset; do i=1 to 10 until(con
Iterator iterator = plugin.inreview.keySet().iterator(); while (iterator.hasNext()) { Player key
晚上好我有一个简单的问题,我警告你我是序言的新手。假设有三个相同大小的列表,每个列表仅包含 1、0 或 -1。我想验证对于所有 i,在三个列表的第 i 个元素中,只有一个非零。 此代码针对固定的 i
我在 scheme 中构建了一个递归函数,它将在某些输入上重复给定函数 f, n 次。 (define (recursive-repeated f n) (cond ((zero? n) iden
我是一名优秀的程序员,十分优秀!