boost boost()后比较嵌套迭代器

时间:2017-06-28 01:09:46

标签: c++ c++11 boost-range boost-adaptors

# !/bin/bash

#Declare Variables
NUM_NOW=`ls -1 /Volumes  | awk '{ cnt++ }END { print cnt }'`
CUR_FD=`find /Volumes -maxdepth 1 -mindepth 1 -type d | cut -d / -f 3`
GET_IP=$(ifconfig en1 | awk '$1 == "inet" { print $2 }')
GET_USR=$(whoami)
VAL_FD=$(ls -l /Volumes | grep -o SAMPLE)

#Checks if the Flash drive is valid
if [[ $VAL_FD = "SAMPLE" ]]; then
  echo "${CUR_FD} IS valid flash drive"
exit 0
elif [[ "$NUM_NOW" -gt "1" ]]; then
  echo "${CUR_FD} IS INSERTED PLEASE REMOVE IT "
  osascript -e 'display notification "Successfully ejected the Flash Drive" with title "Personal Storage was detected"'
  mail -s "Personal Storage was Detected on this IP ${GET_IP}" sample@gmail.com

  hdiutil detach -force /Volumes/${CUR_FD}
fi

如果我使用vector<vector<int>> input{ { { 1, 2 },{ 3, 4 } } }; auto result = input | boost::adaptors::transformed([](const auto& _) {return _; }); result.begin()->begin() == result.begin()->end(); 运行此w / VS2015,则会在_ITERATOR_DEBUG_LEVEL=2中触发此错误:

_Compat(const _Myiter& _Right)

这很重要,因为Flattening iterator _DEBUG_ERROR("vector iterators incompatible"); 中使用了这种比较。

发生了什么事?我该如何解决?

2 个答案:

答案 0 :(得分:2)

这会返回_[](const auto& _) {return _; }的副本。

我没有查看代码,但如果迭代器在每个取消引用上应用转换,那么它根本不会让我感到惊讶,这意味着每次取消引用result.begin()({{{} 1}})你得到了一个不同的矢量副本。迭代器进入不同的向量是不可比的。

答案 1 :(得分:-2)

无论此代码尝试比较地址的行为,我都会假设初始化迭代器vector<vector<int>>::iterator以简化比较存储的第一个和最后一个值的过程第一个子集,最后一个子集应该使用->rbegin()->end()-1而不是->end()来解决,这会带来未分配的内存范围。

让我重新填充你的数组, vector<vector<int>> input{ { { 1, 2 },{ 3, 1 } } };

你要么做其中一个:

assert(*(result.begin()->begin()) == *(result.begin()->end() - 1));
assert(*(result.begin()->begin()) == *(result.begin()->rbegin()));

自1 = / = 2

以来都抛出异常

试试这个:

assert(*(result.begin()->begin()) == *((result.end() - 1)->end() - 1));

它没有,因为最重要的是等于forelast存储的整数。

如果还有其他事情没有在这里谈到或者没有被清楚地理解,请告诉我。