c++ - Vector subscript out of range (but not really) -
I am using a function to fill the random template (test using doubles) vector:
template & lt; Typename T & gt; Zero set list (vector & lt; T & gt; and list) {srand (time (tap)); Int how much, maximum, minimum; Cout & lt; & Lt; "How many items should be generated in the list?"; Cin & gt; & Gt; How many; Cin.ignore (); Cout & lt; & Lt; "Maximum value of the list?"; Cin & gt; & Gt; Maximum; Cin.ignore (); Cout & lt; & Lt; "Minimum value of the list?"; Cin & gt; & Gt; Minute; Cin.ignore (); theList.resize (howmany); T Rand Valley; For (Int i = 0; I & lt; howMany; ++ i) {RAND VALUE = ((((double) RAND () / (double) RAITMX) * (max-min)) + minute; The List [i] = Rand Value; }} The function is said on the vector in the main:
vector & lt; Double & gt; Lista; Setlist (Lista); No matter how much "how much" is the price, how do I access the subscript2, I am getting a subscript from the boundary error.
Step through the debugger, my index is within the subscript range. For example, if a user enters for 10, then when trying to enter the loop
theList [8] = randValue;
">
I have tried this code in Solaris environments and I have not got out of the category index exception.
This is my code: / P>
template> Typnem T & G; zero set list (standard :: vector & lt; T & gt; & amp; list) {srand (time (tap)); int how many, Max, Min; Std :: cout > How many; std :: cin.ignore (); Std :: cout & lt; & lt; "Maximum value of the list?"; Std :: cin> Max; std :: cin.ignore (); Std :: co Ut pre> and my main () looks p>
int main (int argc, char ** argv) {std :: vector & lt; Int & gt; Lista; Lista.clear (); Setlist (Lista); } Code> pre> My output is: p>
Well I have seen that if you define a vector of an integer, the vector material is always the lowest value. The reason for this is that you need to type T to double the intermediate values and to type the result at the end (in the integer type the "/" operator is interpreted as integer split).
You can do this. (<) For (int i = 0; i & lt; howMany; ++ i) {randValue = static_cast & lt; T & gt; (Static_cast (T) Rand ()) / Static_cast and Lt; Double & gt; ((T) RAND_MAX)) * (Maximum - Minimum)) + Minute; List [i] = rand value; }
Comments
Post a Comment