Thread: operating with vectors

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    22

    operating with vectors

    Hi all
    I have a2d vector say
    vector<vector<int> > V ( 5,vector<int>(5)); // 25 size

    Suppose i want to do this operation. if the last element in vector[i] and the first element of v[i+1] differ by a value greater than 20..., i want to push the v[i+1].front() element into v[i].back() position in the array.
    how to do it?

    i wrote a code like this but got segmentation error. please help me how to design this one!!

    Code:
    int main (void)
    {
    	vector <vector<int> > v (5,vector<int>(5));
    	vector<int>::iterator it;
    	int rnd;
    	for (int i=0;i<5;i++)
    	{
    		for ( int j=0;j<5;j++)
    		{
    		rnd = rand() % 100;
    		v[i][j]= rnd;
    		//cout<<v[i][j]<<" ";
    		}
    	}
    	
    	for (int i=0;i<5;i++)
    	{
    		if ( ( (v[i].back() )-(v[i+1].front() ) ) >= 20)
    		{
    			//cout<<"ok!"<<endl;
    				v[i].push_back(v[i+1].front());
    				v[i].resize(v[i].size()+1);
    				v[i+1].resize(v[i+1].size()-1);		
    		}
    	}
    return 0;
    }
    i know there is a flaw but the logic iw ant to achieve is simple. please suggest how it shud be rewrote.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It seems that you have a segfault because this for loop takes too many steps:

    Code:
    for (int i=0;i<5;i++)
    	{
    		if ( ( (v[i].back() )-(v[i+1].front() ) ) >= 20)
    		{
    			//cout<<"ok!"<<endl;
    				v[i].push_back(v[i+1].front());
    				v[i].resize(v[i].size()+1);
    				v[i+1].resize(v[i+1].size()-1);		
    		}
    	}
    Trying to push_back() elements from a vector nonexistent. Actually, you are accessing another, inner vector outside of the outer vector's bounds. To fix it you have to keep i + 1 in bounds. Related to the problem, you need to stop using magic numbers for your for loops and stuff. The size() function is fast and appropriate for controlling for loops.

    Also, I can't figure out why you are resizing. What are you trying to do there?
    Last edited by whiteflags; 06-03-2010 at 12:22 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You don't push_back and then resize by size()+1 push_back already increases the size of the container. You should also use pop_front.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    22
    OK i got your point. But i tried this way. i gives me a segmentation fault...whats the problem here can any1 help me??
    I will be greatful.

    Code:
    #include <iostream>
    #include <vector>
    #include <time.h>
    using namespace std;
    
    int main (void)
    {
    	time_t tm;
    	time(&tm);
    	srand(tm);
    	vector <vector<int> > v (5,vector<int>(5));
    	vector<int>::iterator it;
    	int rnd;
    	for (int i=0;i<5;i++)
    	{
    		for ( int j=0;j<5;j++)
    		{
    		rnd = rand() % 100;
    		v[i][j]= rnd;
    		//cout<<v[i][j]<<" ";
    		}
    	}
    	int k;
    	for (int i=0;i<4;i++)
    	{
    		while (abs( ( (v[i].back() )-(v[i+1].front() ) ) < 30))
    		{
    				k = v[i+1].front();
    				v[i+1].erase(v[i+1].begin() );
    				v[i].push_back(k);	
    		}
    
    	}
    	
    	for (int i=0;i<v.size();i++)
    	{
    		//cout<<v[i].size()<<" ";
    		for(int j=0;j<v[i].size();j++)
    		{
    			cout<<v[i][j]<<" ";
    		}
    		cout<<endl;
    	}
    	cout<<endl;
    return 0;
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This modification works for me:
    Code:
    	for (int i=0;i<4;i++)
    	{
    		auto BackE = v[i].back();
    		auto FrontE = v[i+1].front();
    		if (abs(BackE - FrontE) < 30)
    		{
    			k = v[i+1].front();
    			v[i+1].erase(v[i+1].begin() );
    			v[i].push_back(k);	
    		}
    	}
    I don't know if the program is 100% right, but I believe the output seemed correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    22
    No i tried IF condition... but u see i need the elements to be pushed into prev array as long as they are less than 30 of difference. if does it once... can you suggest how can handle continous shifts using while?
    thanks

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If you want to change it so that the same vectors will be compared, lengthened, and shortened respectively, you can do that. It's just that you won't bump the loop count until the comparison between the two elements fails. The abs(v[i].back () - v[i + 1].front ()) < whatever comparison is still only a comparison.

    The output might look like this:
    Code:
    vector[0] = ( 37, 20, 32, 70, 63, ).
    vector[1] = ( 31, 95, 49, 93, 38, 24, 30, 12, ).
    vector[2] = ( 90, 74, ).
    vector[3] = ( 36, 83, 27, 79, 74, 70, 51, 60, 55, ).
    vector[4] = ( 96, ).
    Isn't that what you expect?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You also have to be careful of what happens if a vector gets reduced to being empty.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    22
    yeah thanks.. thats what i expect... but how to handle it when the vector becomes empty... i want the next set of vector to be pushed forward... can you suggest what to code when the size of v[i] becomes 0.... i want to remove it... how to do it using iterators?

    please suggest!!
    thanks
    regards,
    KR

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    22
    i got the segment fault only becuz the size of the vecotr became zero and the system dint know what to do...
    I want help in given telliing the system to move forward when the size of i+1 becomes zero...
    regards

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Test for v[i + 1].empty()
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    22
    Hey i tried the problem using this.. can you tell me if the flow of logic is right in my steps?.. would appreciate any help...
    Code:
    do 
    		{
    			if (abs( v[i].back() - v[i+1].front() ) < 5)
    			{
    				k = v[i+1].front();
    				v[i].push_back(k);
    				v[i+1].erase(v[i+1].begin() );
    				i++;	
    			}
    		}while ((i<5) && (!v[i].empty()) ); // 5 is the size of the vector of vectors.. 5*5 as i said before...

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    This modification works for me:
    Code:
    	for (int i=0;i<4;i++)
    	{
    		auto BackE = v[i].back();
    		auto FrontE = v[i+1].front();
    		if (abs(BackE - FrontE) < 30)
    		{
    			k = v[i+1].front();
    			v[i+1].erase(v[i+1].begin() );
    			v[i].push_back(k);	
    		}
    	}
    I don't know if the program is 100% right, but I believe the output seemed correct.
    Auto? Isn't that C++0x only?
    I love the feature though, it's going to make some things a lot easier!

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is. There are available compilers that supports it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    Yes, it is. There are available compilers that supports it.
    Yes, though I don't think it's a good idea suggesting to use it to people who don't fully know what they're doing...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using/ Searching through Multiple Vectors
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 08-03-2009, 08:35 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  4. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  5. Why Can't C++ Be Used to Develop Operating System?
    By Antigloss in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2005, 06:16 AM