@@ -594,10 +594,7 @@ namespace rapidcsv
594594
595595 for (auto itRow = _mData.begin (); itRow != _mData.end (); ++itRow)
596596 {
597- if (columnIdx < itRow->size ())
598- {
599- itRow->erase (itRow->begin () + static_cast <ssize_t >(columnIdx));
600- }
597+ itRow->erase (itRow->begin () + static_cast <ssize_t >(columnIdx));
601598 }
602599
603600 _updateColumnNames (" rapidcsv::Document::RemoveColumn()" );
@@ -652,21 +649,22 @@ namespace rapidcsv
652649 _mData.push_back (row);
653650 }
654651
652+ for (auto itRow = _mData.begin (); itRow != _mData.end (); ++itRow)
653+ {
654+ if (pColumnIdx > itRow->size ())
655+ {
656+ const std::string errStr = " column out of range: " +
657+ std::to_string (pColumnIdx) + " (on row " +
658+ std::to_string (std::distance (_mData.begin (), itRow)) +
659+ " )" ;
660+ throw std::out_of_range (errStr);
661+ }
662+ }
663+
655664 size_t rowIdx = 0 ;
656665 for (auto itRow = _mData.begin (); itRow != _mData.end (); ++itRow, ++rowIdx)
657666 {
658- if (pColumnIdx <= itRow->size ())
659- {
660- itRow->insert (itRow->begin () + static_cast <ssize_t >(pColumnIdx), column.at (rowIdx));
661- }
662- else
663- {
664- const std::string errStr = " column out of range: " +
665- std::to_string (pColumnIdx) + " (on row " +
666- std::to_string (std::distance (_mData.begin (), itRow)) +
667- " )" ;
668- throw std::out_of_range (errStr);
669- }
667+ itRow->insert (itRow->begin () + static_cast <ssize_t >(pColumnIdx), column.at (rowIdx));
670668 }
671669
672670 if (_mLabelParams.mColumnNameFlg == FlgColumnName::CN_PRESENT)
@@ -885,7 +883,6 @@ namespace rapidcsv
885883 const std::string& rowName = _mIdxRowNames.at (i);
886884 _mRowNamesIdx[rowName] = i;
887885 }
888-
889886 }
890887
891888 _mData.erase (_mData.begin () + static_cast <ssize_t >(pRowIdx));
0 commit comments