就变量命名约定而言,是应该命名迭代器i还是更像语义的东西count?如果你不使用i,为什么不呢?如果您认为这i是可以接受的,是否存在不应该使用迭代的情况?
经过几年的C#和最近的Objective C,我刚刚回到C++.
我之前做过的一件事就是为std :: map滚动我自己的迭代器适配器,它将仅仅反映值部分,而不是键值对.这是一个非常普遍和自然的事情.C#为此工具提供了其Dictionary类的Keys和Values属性.类似地,Objective-C的NSDictionary具有allKeys和allValues.
自从我"离开"以来,Boost已经收购了Range和ForEach库,我现在正在广泛使用它们.我想知道两者之间是否有一些设施可以做同样的事情,但我找不到任何东西.
我正在考虑使用Boost的迭代器适配器来解决问题,但在我沿着这条路走下去之前,我想我会问这里是否有人知道Boost中的这样一个设施,还是其他现成的设施?
我有许多大文件,我想处理除了每个文件中的最后一行以外的所有文件.如果文件很小,我可以转换为TraversableLike并使用"init"方法,例如:
lines.toList.init
Run Code Online (Sandbox Code Playgroud)
但是文件很大所以我需要将事物保存为迭代器.有没有一种简单的方法可以在迭代器上获得类似"init"的内容?我正在考虑以下内容,但我不相信它会一直有效:
lines.takeWhile(_ => lines.hasNext)
Run Code Online (Sandbox Code Playgroud) 对于字典,我可以iter()用来迭代字典的键.
y = {"x":10, "y":20}
for val in iter(y):
print val
Run Code Online (Sandbox Code Playgroud)
当我有迭代器如下,
class Counter:
def __init__(self, low, high):
self.current = low
self.high = high
def __iter__(self):
return self
def next(self):
if self.current > self.high:
raise StopIteration
else:
self.current += 1
return self.current - 1
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样使用它
x = Counter(3,8)
for i in x:
print x
Run Code Online (Sandbox Code Playgroud)
也不
x = Counter(3,8)
for i in iter(x):
print x
Run Code Online (Sandbox Code Playgroud)
但这样呢?
for c in Counter(3, 8):
print c
Run Code Online (Sandbox Code Playgroud)
功能的用途是iter()什么?
我想这可能是如何iter() …
我在std :: string对象中有一个文本.该文由几行组成.我想使用STL(或Boost)逐行迭代文本.我提出的所有解决方案似乎都不是很优雅.我最好的方法是在换行符处拆分文本.有更优雅的解决方案吗?
更新:这就是我要找的:
std::string input;
// get input ...
std::istringstream stream(input);
std::string line;
while (std::getline(stream, line)) {
std::cout << line << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我以为我已经尝试过了.我收到了一个编译错误并把它丢了.快点!
我们正在使用另一家供应商的库,这显然是使用错误的标志编译的,即32位调试模式下的_ITERATOR_DEBUG_LEVEL = 0.虽然我已经向他们提交了错误报告,但我需要一个中间解决方案.我们不自己使用stl,所以我可以自由地为使用该库的子项目更改此标志.但我无法弄清楚如何做到这一点.我尝试过的东西不起作用:
/D_ITERATOR_DEBUG_LEVEL=0
> LINK : warning LNK4044: unrecognized option '/D_ITERATOR_DEBUG_LEVEL=0'; ignored
#define _ITERATOR_DEBUG_LEVEL 0
> Nothing happens
Run Code Online (Sandbox Code Playgroud)
在没有检查迭代器的情况下,使项目编译的正确语法或选项是什么?
独立的STL算法(如std::count_if)采用一对迭代器.在我使用这些的所有情况下(以及我在网上看到的所有例子中),我发现自己打字了
std::count_if(myContainer.begin(),myContainer.end(), /* ... */ );
Run Code Online (Sandbox Code Playgroud)
是否有为什么样式的速记模板的原因
std::count_if(myContainer, /* ... */ );
Run Code Online (Sandbox Code Playgroud)
鉴于在整个集装箱上进行的操作不是很多而没有提供?我只是忽略了它吗?答案是否与c ++ 11和c ++ 03不同?
这是这个问题的完全重复,除了接受的答案是错误的,所以我再问一遍:
如何正确检查给定类型T是否为迭代器?
我尝试解决它:
// Assume enable_if and is_same are defined
// (hoping for a solution that works for C++03 too)
template<class T>
class is_iterator
{
static char (&test(...))[2];
template<class U>
static typename std::enable_if<
!std::is_same<
typename std::iterator_traits<T>::value_type,
void
>::value,
char
>::type test(U);
public:
static bool const value = sizeof(test(0)) == 1;
};
struct Foo { };
int main()
{
return is_iterator<Foo>::value;
}
Run Code Online (Sandbox Code Playgroud)
在Visual C++上碰巧失败了:
...\vc\include\xutility(373):
错误C2039 ::'iterator_category'不是成员'Foo'
因为iterator_traits …
我试图在Java中迭代hashmap,这应该是一件相当容易的事情.但是,以下代码给了我一些问题:
HashMap hm = new HashMap();
hm.put(0, "zero");
hm.put(1, "one");
Iterator iter = (Iterator) hm.keySet().iterator();
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
System.out.println(entry.getKey() + " - " + entry.getValue());
}
Run Code Online (Sandbox Code Playgroud)
首先,我需要在hm.keySet().iterator()上强制转换Iterator,否则它会说"类型不匹配:无法从java.util.Iterator转换为Iterator".但后来我得到"方法hasNext()未定义类型Iterator",并且"方法hasNext()未定义类型Iterator".
我试图找到a的最大像素值cv::Mat.
问题:*maxValue总是回归0.
从这个SO线程,我理解' max_element返回迭代器,而不是值.这就是我使用*maxValue'的原因
cv::Mat imageMatrix;
double sigmaX = 0.0;
int ddepth = CV_16S; // ddepth – The desired depth of the destination image
cv::GaussianBlur( [self cvMatFromUIImage:imageToProcess], imageMatrix, cv::Size(3,3), sigmaX);
cv::Laplacian(imageMatrix, imageMatrix, ddepth, 1);
std::max_element(imageMatrix.begin(),imageMatrix.end());
std::cout << "The maximum value is : " << *maxValue << std::endl;
Run Code Online (Sandbox Code Playgroud)
注意:如果min_element替换代替max_element,则minValue代替maxValue,*minValue将始终返回0.