C++ vector help

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

C++ vector help

Post by 1Seraphim1 on Tue Mar 12, 2013 10:59 pm
([msg=74484]see C++ vector help[/msg])

Hi forum, I was wondering if someone could help me with my code. I am trying to code a program that compares two integer vectors to see if they have the same set of numbers regardless of their sequence. The code needs at least one vector and one boolean function to determine if the vectors have the same set of numbers. This is what i have so far to test if their size is the same. Can someone explain why the code runs, but then crashes?


#include <iostream>
#include <vector>
using namespace std;


bool same_set(vector<int> a, vector<int> b)
{

if(a.size() != b.size()) //Verify that the vectors are the same size.
return false;
else return true;

}

int main()
{
int value;
vector <int> a;
cout << "Please enter a series of numbers: ";
while(!cin.fail())
{
a.push_back(value);
}

return 0;

}
1Seraphim1
New User
New User
 
Posts: 2
Joined: Sun Mar 03, 2013 10:00 pm
Blog: View Blog (0)


Re: C++ vector help

Post by 323LORDofWAR323 on Wed Mar 13, 2013 12:11 am
([msg=74487]see Re: C++ vector help[/msg])

Well i may be stating the obvious but I ran the program and opened task manager
The memory skyrockets and then the program crashes
so your problem is in the while loop because that process is running over and over causing your program to crash...

also instead of using "int" value wouldn't "float" be more appropriate?

just some input
good luck!
323LORDofWAR323
New User
New User
 
Posts: 8
Joined: Fri Mar 08, 2013 7:56 pm
Blog: View Blog (0)


Re: C++ vector help

Post by WallShadow on Wed Mar 13, 2013 6:46 pm
([msg=74516]see Re: C++ vector help[/msg])

I don't know, but as it seems to me, the problem is very obvious;

Code: Select all
    while(!cin.fail())
    {
          a.push_back(value);
    } 


all you are doing is infinitely pushing value into the array vector, you aren't reading any value into it value so there is nothing to slow it down from infinitely storing the same value over and over again until it runs out of memory.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 594
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: C++ vector help

Post by 5ilic0n on Thu Apr 18, 2013 2:35 am
([msg=75203]see Re: C++ vector help[/msg])

So do you absolutely need to use vectors? Because I have a STL map solution that is super efficient. :)
5ilic0n
New User
New User
 
Posts: 22
Joined: Sat Apr 06, 2013 3:08 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests