程序适用于小迭代,但在大迭代期间将向量下标扔出范围

时间:2018-07-12 04:40:23

标签: c++ exception

这是我的代码。我试图找到最小距离(指数差异)b / w 2个相等的元素:

#include<iostream>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>

using namespace std;

vector<string> split_string(string);
int n;
int minimumDistances(vector<int> a) {
int mn=n,flag=0,mch,pos,diff=0;
for (int i = 0;i < n-1;i++) {
    //mch = a[i];
    for (int j = i + 1;j < n;j++) {

        if (a[j]==a[i]) {
            flag = 1;
            pos = j;
            diff = j - i;
        }
    }
    if (diff<mn) {
        mn = diff;
    }
}
if (flag==0) {
    return -1;
}
return mn;
}

int main()
{


cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

string a_temp_temp;
getline(cin, a_temp_temp);

vector<string> a_temp = split_string(a_temp_temp);

vector<int> a(n);

for (int i = 0; i < n; i++) {
    int a_item = stoi(a_temp[i]);

    a[i] = a_item;
}

int result = minimumDistances(a);
cout << result;

cin.get();
return 0;
 }

 vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), 
[](const char &x, const char &y) {
    return x == y and x == ' ';
});

input_string.erase(new_end, input_string.end());

while (input_string[input_string.length() - 1] == ' ') {
    input_string.pop_back();
}

vector<string> splits;
char delimiter = ' ';

size_t i = 0;
size_t pos = input_string.find(delimiter);

while (pos != string::npos) {
    splits.push_back(input_string.substr(i, pos - i));

    i = pos + 1;
    pos = input_string.find(delimiter, i);
}

splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 
 1));

return splits;
 }

该异常发生在箭头标记的pt处 当n = 10时没有错误 当= 1000发生异常时

为什么这种例外只发生在较大的值上?

样本输入:

39572 89524 21749 94613 75569 74800 91713 62107 28574 22617 22271 22624 28116 67573 53717 9358 65220 59894 78686 10945 33641 11708 8851 11860 66780 64697 799 47782 41971 54170 8960 81543 60047 47061 92508 51968 38213 84221 14075 66787 23191 52698 5764 51307 20271 59481 77018 1843 19375 55704 12789 53016 83764 37992 64877 50545 19041 82028 98327 61012 52551 7287 42555 12598 70700 51416 80918 8914 35637 11345 75701 58828 80395 97817 26488 17019 57299 3506 18862 93026 75562 48003 62395 59327 85996 27272 9872 5037 25652 8199 82402 78203 31838 41309 88071 27804 28363 99416 19858 3543 79812 17675 30031 96831 91326 49889 15693 84353 25452 80049 46748 84779 66045 90372 94651 87434 16024 19202 69836 94228 67392 27498 1381 86282 20223 5805 14087 48586 5221 50297 68482 85033 67972 98513 98216 59299 48403 30262 60004 73855 10311 13476 85989 96494 29500 5191 82683 40080 88935 10181 57814 75217 30404 63619 5656 95343 68840 55953 63825 70226 23926 62338 68442 9957 7 27093 15056 59581 17300 25367 82685 92286 34427 96161 78275 30922 25661 99818 13605 82094 88753 23786 39908 80323 54190 3527 85979 65885 72367 41933 29710 58945 82211 8401 43740 81788 35494 58796 57721 69147 516 40406 77785 34943 36567 72413 65865 78580 72231 95822 60674 77337 57660 6503 4109 43639 72388 92829 1924 2099 51774 84135 10500 95514 82275 62346 70663 39996 31493 71179 80402 9279 6122 16969 81692 88340 11902 70275 514 72576 47612 36475 73159 21624 42978 93620 65264 15366 86449 67188 17465 54576 67676 44317 50090 49951 6664 20753 89948 54509 80140 14407 20024 78184 19099 31926 48460 19613 4502 12424 72440 94013 34049 15418 87634 99313 30785 90435 82853 64602 45011 50529 25272 11454 481 48288 48559 6781 2797 56844 93483 82938 87603 13507 61122 6702 45433 25934 42667 66288 38359 15108 60301 72408 46878 54287 88278 940 74970 99734 37808 242 27540 54641 48530 76100 61422 67679 32944 54905 66969 20547 84765 44444 43601 30198 86730 2620 12838 250 89 17728 73140 13849 64607 53779 18274 74974 8502 5553 49944 24589 59713 50186 52129 14354 15068 28229 75776 99100 77525 47033 66069 98072 31798 26865 41673 78349 13596 44294 7539 55037 78374 80679 68887 59333 50811 3513 34308 59313 9066 84252 254 85131 50791 52384 99613 82613965 81311 74491 38647 63733 88915 86797 90598 46941 65146 20546 7587 72686 75584 85961 69717 60823 61647 36880 64336 12307 96194 89755 12911 12800 74886 63702 65184 90724 45914 78502 82337 43577 52993 20984 7310 41908 24134 97909 88849 5632 18455 96436 94670 10391 98750 80740 71214 60397 89056 30166 41658 1967 42967 32896 65670 8151 23620 27936 86653 5958 71513 55998 43294 78824 14259 67428 93085 19460 73061 27892 15897 84083 54636 30999 64823 25850 7748 82444 77753 96804 12610 35763 98771 55577 68660 80793 80081 92280 8729 83086 14590 96595 55437 57885 89156 31078 29100 21405 15162 83736 52404 79985 25939 60152 78781 3692 73308 7744 39456 72080 63321 24468 69225 59754 3 3100 94307 42841 47691 90902 98278 21928 82673 84326 63593 83881 73482 11024 29333 94888 42538 29422 63644 38875 71713 40149 17657 75405 13457 25401 31213 1889 5074 72033 71115 64829 5134 65422 24022 52825 72676 22300 91105 71701 6626 54698 55582 96460 82074 1267 7700 24612 47041 18754 11494 97497 10512 41303 39250 58077 59545 44324 30111 47012 25505 35245 28786 49527 4422 1462 88179 95527 73163 11157 66577 45097 76081 48652 62716 31670 89616 9758 3015 69456 28512 30861 83305 39024 88517 22555 97102 48062 83232 43565 95074 8737 78810 2386079665617 14837 90306 61688 59934 14276 26692 22650 45946 32661 48760 48962 2117 77273 96175 1775 32649 84692 24330 46103 32754 23914 89668 44180 49004 84830 84392 23621 84414 9714 2769 95877 40903 93075 73918 17189 7189 16962 56192 53298 49623 4952 18612 68093 98577 31139 69868 47579 10832 64938 34465 99703 25471 83469 885 9863 23442 1652 35930 26211 97529 76833 35639 87799 94023 59342 4762 50215 28992 5438 5 71519 47604 38830 70097 78744 8698 17676 10928 19249 27710 92218 70066 27413 17689 53535 44651 27553 76977 46303 63483 19540 43832 56668 55179 31632 50691 30874 52746 17258 59866 7131 5130 23823 45962 75227 18919 71012 9255 29847 6613 53317 38417 76679 23660 56107 46566 41734 88037 63495 59436 48221 20163 30967 96205 87207 61841 48951 20817 38060 56083 25947 61883 18397 17526 80802 5761 26781 27001 12375 96451 81770 89054 93534 37877 35621 35268 37889 75516 23305 1384 34952 71526 21548 82272 67732 25107 44113 16683 45924 98525 89118 71872 60408 23629 5750 48884 915 42004 45335 82686 47410 38869 36915 83031 74137 74805 74900 97442 76189 26204 85320 14089 8476 69404 55548 68942 2440 1473 67467 7910 89697 44228 31778 95447 1790 77759 44331 19058 36115 89666 18096 83525 44887 55011 82909 19024 29816 57809 32818 22358 365 34491 52799 8348 94136 22687 26173 61603 30598 15870 5831 78728 27669 23974 56487 88353 59384 92602 94371 77480 92479 39259 32491 91740 58283 78660 65901 91102 1018 66267 25593 70169 91461 45840 78517 85597 68528 4690 47200 99126 36912 69384 94206 64582 93358 50693 52935 52742 59647 47306 46574 68478 86565 95417 60219 44849 74077 26120 52303 91447 8739 94248 61617 200 40088 40134 85797 8616 61177 32998 24094 98089 18734 62671 12092 1697 31958 81186 61344 79265 44112 46175 82182 39529 22746 43383 13607 65218 95686 5054 73958 6286 66671 74158 46375 23158 76308 71343 84335 25658 11790 82424 44392 46442 61448 72836 48140 93406 70374 25374 89023 14486 88363 71206 54015 11109 14589 83974 76974 26328 89914 72052 57148 95641 11562 33456 66985 95897 59114 95127 94674 19858 57921 56122 92694 60061 65880 79420 48250 54904 54906 93906 36613 26110 64274 64075 57051 48248 40403 31 37277 7041 49298 25682 64189 44939 37244 13998 28998 49276 89494 89464 23403 44168 9323 97677 16642 18738 3738 82522 97790 68 / p>

0 个答案:

没有答案