假设我有一个it指向map.
我还有另一个迭代器it1,我想做这样的事情
it1 = it + 1;
Run Code Online (Sandbox Code Playgroud)
我们如何在 C++ 中实现这一点,因为上面的语句在 C++ 中给出了错误。
我遇到了很奇怪的问题
对于这样的代码
template <typename T>
struct A{
explicit A(unsigned int size = 0, const T &t = T())
{
}
template <typename InputIterator>
A(InputIterator first, InputIterator last) {
for(;first != last ; ++first)
{
*first; //do something with iterator
}
}
};
Run Code Online (Sandbox Code Playgroud)
例如,当我定义
A<int> a(10,10);
Run Code Online (Sandbox Code Playgroud)
使用迭代器的第二个构造函数而不是第一个构造函数。那么当向量构造函数看起来很漂亮时,它们是如何工作的呢?
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
Run Code Online (Sandbox Code Playgroud)
我可以制作向量 v(10,10) 而不会有任何麻烦。
PS我有这样的错误
temp.cpp: In instantiation …Run Code Online (Sandbox Code Playgroud) 如果我有一个包含整数对的集合,
set<pair<int,int> > cells;
Run Code Online (Sandbox Code Playgroud)
如何使用“查找”查找集合中是否存在一对。我可以使用“查找”来设置一个值,但不能为一对设置。
我正在尝试,
cells.insert(make_pair(1,1));
set<int,int>::iterator it;
it=cells.find(pair<int,int>(1,1));
error: no match for 'operator=' in 'it = cells.std::set<_Key, _Compare, _Alloc>::find<std::pair<int, int>, std::less<std::pair<int, int> >, std::allocator<std::pair<int, int> > >((*(const key_type*)(& std::pair<int, int>((* &1), (* &1)))))'|
Run Code Online (Sandbox Code Playgroud)
有没有人有任何想法?谢谢!
你好。我正在尝试运行以下代码(仅用于培训目的):
#include<iostream>
#include <list>
template<class T,
template<class ,class=std::allocator<T> >class kont >
typename std::iterator_traits<T>::value_type foo_test(typename kont<T>::iterator b){return *b;}
template <class Iter>
typename std::iterator_traits<Iter>::value_type minimum(Iter b, Iter e)
{
Iter m = b;
/*
CODE
*/
return *m;
}
int main(void){
std::list<int> x;
x.push_back(10);
x.push_back(100);
std::cout <<minimum(x.begin(),x.end());
//std::cout <<foo_test<int,std::list>(x.begin());
}
Run Code Online (Sandbox Code Playgroud)
函数 minimum 工作正常,没有问题。但是,当我取消注释最后一行时,我收到以下错误:
main.cpp:33:50: error: no matching function for call to ‘foo_test(std::__cxx11::list<int>::iterator)’
std::cout <<foo_test<int,std::list>(x.begin());
main.cpp:7:46: note: template argument deduction/substitution failed:
main.cpp:33:50: required from here
main.cpp:7:46: error: no type named ‘value_type’ in ‘struct …Run Code Online (Sandbox Code Playgroud) 我正在尝试dict在 Python 中创建一个类似的类。
当你创建一个类时,你有一些方法告诉 Python 如何创建一个内置类。例如,__int__如果用户int()在类的实例上使用,覆盖该方法会告诉 Python 返回什么。对于__float__. 您甚至可以通过覆盖该__iter__方法来控制 Python 如何生成类的可迭代对象(这可以帮助 Python生成类的lists 和tuples)。我的问题是你如何告诉 Python 如何制作dict你的自定义类?没有什么特别的__dict__方法,你会怎么做呢?我想要类似以下内容:
class Foo():
def __dict__(self):
return {
'this': 'is',
'a': 'dict'
}
foo = Foo()
dict(foo) # would return {'this': 'is', 'a': 'dict'}
Run Code Online (Sandbox Code Playgroud)
我试过让类继承自dict,但由于子类试图继承自dictand type,因此在代码中稍后会引发错误,因此继承自dict是不可能的。有没有其他方法可以做到?
此外,我已经覆盖了该__iter__方法,以便它返回一个dict_keyiterator对象(iter()在 a 上使用时返回的内容dict),但它似乎仍然无法正常工作。
我正在寻找最好的方法String来Windows<T>使用windows为切片提供的功能.
我理解如何以这种方式使用Windows:
fn main() {
let tst = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
let mut windows = tst.windows(3);
// prints ['a', 'b', 'c']
println!("{:?}", windows.next().unwrap());
// prints ['b', 'c', 'd']
println!("{:?}", windows.next().unwrap());
// etc...
}
Run Code Online (Sandbox Code Playgroud)
但是在处理这个问题时我有点迷失:
fn main() {
let tst = String::from("abcdefg");
let inter = ? //somehow create slice of character from tst
let mut windows = inter.windows(3);
// prints ['a', 'b', 'c']
println!("{:?}", windows.next().unwrap());
// prints ['b', …Run Code Online (Sandbox Code Playgroud) 考虑下面的虚拟示例:
def common_divisors_generator(n, m):
# Init code
factors_n = [i for i in range(1, n + 1) if n%i == 0]
factors_m = [i for i in range(1, m + 1) if m%i == 0]
# Iterative code
for fn in factors_n:
for fm in factors_m:
if fn == fm:
yield fn
# The next line is fast because no code is executed yet
cdg = common_divisors_generator(1537745, 373625435)
# Next line is slow because init code is executed on first …Run Code Online (Sandbox Code Playgroud) public static void main(String args[]) throws JSONException {
JSONObject json = new JSONObject();
json.put("name", "abcgdj");
json.put("no", "1234");
json.put("contact", "6748356");
Iterator<?> keys = json.keys();
System.err.println(Iterators.size(keys));
System.err.println(Iterators.size(keys));
}
Run Code Online (Sandbox Code Playgroud)
在此代码中,执行后Iterators.size(keys),迭代器变为空,对于第二个print语句,它返回0.
该size()方法在包下com.google.common.collect.iterators.所以我看了Iterators.size()函数的代码.它是,
public static int size(Iterator<?> iterator) {
long count = 0L;
while (iterator.hasNext()) {
iterator.next();
count++;
}
return Ints.saturatedCast(count);
}
Run Code Online (Sandbox Code Playgroud)
所以我怀疑迭代器是如何keys变空的.是否通过引用调用?
任何人都可以解释size()函数内发生的事情
我正在尝试实现可以无限迭代的结构。认为它是自然数。我有一个局限性:它不能实现Copy特征,因为结构包含一个String字段。
我还实现了一个Iterable特征及其唯一成员fn next(&mut self) -> Option<Self::Item>。
当前,我有以下代码可以迭代结构的前10个项目:
let mut counter = 0;
let mut game:Option<Game> = Game::new(¶m);
loop {
println!("{:?}", game);
game = g.next();
counter = counter + 1;
if counter > 10 { break; }
}
Run Code Online (Sandbox Code Playgroud)
我想让用户crate能够使用for in构造对我的结构进行迭代,如下所示:
for next_game in game {
println!("{:?}", next_game);
}
Run Code Online (Sandbox Code Playgroud)
有可能吗?我该如何实现?如何使我的代码更好,以及与我的结构有什么关系?
迭代器实现:
pub struct Game {
/// The game hash
pub hash: Vec<u8>
}
impl Iterator for Game {
type Item …Run Code Online (Sandbox Code Playgroud) 我正在编写一种算法,该算法将一些数据写入提供的输出范围(问题的初始文本包括具体内容,并将注释中的讨论变成错误的方向)。我希望它在API中与标准库中的其他范围算法尽可能接近。
我查看了的实例的最新草案std::ranges::output_range,发现只有2种算法:
他们俩都回来了std::ranges::safe_iterator_t。我认为返回是合乎逻辑的std::ranges::safe_subrange_t。即使您写入输出流,在这种情况下,您仍然可以返回迭代器-前哨对,并将该范围向下传递。
我找到了P0970,看起来好像std::ranges::safe_subrange_t是后来添加的。也许算法根本没有更新?还是有其他原因?