GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-May-2007, 23:22
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 520
Peter_APIIT can only hope to improve
Thumbs up

Vector Issues


Hello all C/C++ expert programmer,

i have tremendous problem about vector.
  • What is vector?
  • What is iterator?
  • What the difference between array and vector?

Ans : Array is fixed size.
We cannot copy array.
Vector is a kind of dynamic data
structure. (I don't know
whether this it true because i
just guess)

I have googling it and find in the book but i also couldn't understand. I think i should smash my head to the wall or jump from Taipei 101.

Below is my program :
CPP / C++ / C Code:
#include<iostream>
#include<vector>

using namespace std;

int main(int argc, char *argv[])
{
	int loop;
	vector<int>v1(5), v2(5), result(5);

	while(1)
	{
		for (loop=0;loop<v1.size() && loop<v2.size();loop++)
		{
			cout << "\nEnter a vector1 loop : ";
			cin >>v1[loop];
			cout << "\nEnter a vector2 loop : ";
			cin >> v2[loop];
		}
		for (loop=0;loop<v1.size() && loop<v2.size();loop++)
		{
			result[loop] = v1[loop] + v2[loop];
		}
		for (loop=0;loop<v1.size() && loop<v2.size();loop++)
		{
			cout << "The result is  " << result[loop] << "\n";
		}
	}

	return 0;
}

Why the loop is not stop even though it is over the size ?
How we can prompt user enter a number into vector and reverse the number ?
Example: 1234
Answer:4321

Thanks for your help.

Your help is greatly appreciated by me and others who start learning C++.
Last edited by admin : 13-May-2007 at 09:52. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 13-May-2007, 07:34
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,006
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Vector Issues


The loop will not stop, because you have:
CPP / C++ / C Code:
while(1)
This outer [while] loop is always true, so how do you plan to make it stop? Remove the while-loop and it'll run fine.

EDIT:
Here are a couple of websites that might better explain some of what you seek:
http://www.codeproject.com/vcpp/stl/stlintroduction.asp
http://www.yolinux.com/TUTORIALS/Lin...ialC++STL.html

Reversing the elements can be done something like this:
CPP / C++ / C Code:
for ( vector<int>::reverse_iterator element = result.rbegin(); element < result.rend(); element++ ) 
{
	cout << "(in reverse): " << *element << "\n";
}
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 13-May-2007 at 08:20.
  #3  
Old 13-May-2007, 09:11
davis
 
Posts: n/a

Re: Vector Issues


Quote:
Originally Posted by Peter_APIIT
Hello all C/C++ expert programmer,

i have tremendous problem about vector.

1. What is vector ?

A vector is a Sequence that supports random access to elements, constant time insertion and removal of elements at the end, and linear time insertion and removal of elements at the beginning or in the middle. The number of elements in a vector may vary dynamically; memory management is automatic. Vector is the simplest of the STL container classes, and in many cases the most efficient.

...source: www.sgi.com

Quote:
Originally Posted by Peter_APIIT
2. What is iterator ?

Iterators are a generalization of pointers: they are objects that point to other objects. As the name suggests, iterators are often used to iterate over a range of objects: if an iterator points to one element in a range, then it is possible to increment it so that it points to the next element.

...source: www.sgi.com

Quote:
Originally Posted by Peter_APIIT
3. What the difference between array and vector ?

Ans : Array is fixed size.
We cannot copy array.
Vector is a kind of dynamic data
structure. (I don't know
whether this it true because i
just guess)

You can copy an array, but you have to know a lot about it. You are also responsible for the memory allocation.

Vector is not a dynamic data structure. It is a Sequence (a generic Container) of elements owned by the vector. The elements are dynamically allocated by the vector during storage. They are also automatically deallocated by the vector upon removal from the container.

Quote:
Originally Posted by Peter_APIIT
How we can prompt user enter a number into vector and reverse the number ?
Example: 1234
Answer:4321

Try this:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef std::vector<int>vInts;

int main()
{
    vInts v;
    cout << "Enter an integer: ";
    string s;
    cin >> s;
    for( int i = 0; i < (int)s.size(); i++ )
    {
	v.push_back(  s[i] - 0x30 );
    }

// iterator method

    vInts::iterator it;
    // forward
    for( it = v.begin(); it != v.end(); it++ )
    {
	cout << (*it);
    }
    cout << endl;

    vInts::reverse_iterator rev_it;
    // reverse
    for( rev_it = v.rbegin(); rev_it < v.rend(); rev_it++ )
    {
	cout << (*rev_it);
    }
    cout << endl;

// simpler but, non-iterator method

    // forward
    for( int i = 0; i < (int)v.size(); i++ )
    {
	cout << v[i];
    }
    cout << endl;

    // reverse
    for( int i = v.size() -1; i >= 0; --i )
    {
	cout << v[i];
    }
    cout << endl;

    return 0;
}


Code:
Enter an integer: 1234 1234 4321 1234 4321 Enter an integer: 2847372646585476261153745903928173645252713957676 2847372646585476261153745903928173645252713957676 6767593172525463718293095473511626745856462737482 2847372646585476261153745903928173645252713957676 6767593172525463718293095473511626745856462737482


:davis:
  #4  
Old 14-May-2007, 02:25
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 520
Peter_APIIT can only hope to improve
Thumbs up

Re: Vector Issues


I feel shameful because always ask simple question rather than do my own research.

By the way, all the explanation is short and simple. This had make me understand easier and have not feel frustrated to learn C++.

Thanks.
Your help is greatly appreciated by me and others.

A billion thanks to you.
  #5  
Old 14-May-2007, 04:57
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 520
Peter_APIIT can only hope to improve
Thumbs up

Re: Vector Issues


My program has some problem. The loop is not stop after the size.

Below is my program :

CPP / C++ / C Code:
// A program that follow STL
// Iterator is a generalization of pointer
// Iterator is a object pointer pointing to
// another object.

#include<iostream>
#include<vector>

using std::cout;
using std::cin;
using std::endl;


// For vector and iterator;
using std::vector;
using std::iterator;

int main(int argc, char *argv[])
{
	int size = 5;
	int loop;

	cout << "Enter a integer size : ";
	cin >> size;

	vector<int>vec(size); // A vector has run-time size
	
	// Prompt user for input
	for (loop=0;loop<vec.size();++loop)
	{
		cout << "\nEnter integer : ";
		cin >> vec[loop];
//		vec.push_back(vec[loop]);
	}// The loop is not stop here
	// Display the user input using 
	// iterator method

	vector<int>::iterator itera;

	for (itera=vec.begin();itera!=vec.end();itera++)
	{
		cout << "\nThe integer is " << (*itera);
	}
	
	// reverse Iterator
	vector<int>::reverse_iterator rev_it;

	for (rev_it=vec.rbegin();rev_it!=vec.rend();rev_it++)
	{
		cout << "\nThe reverse integer is " << (*rev_it);
	}
	return 0;

}

I have a question.

1. If i have class A[10], i would like
iterate the object A[10].

I will declare A::iterator itera;

2. If i have list<int>aList, i would like
iterate the list.

I will declare aList::iterator itera;

I don't know whether this is true because i just guess.

Thanks.
Last edited by admin : 14-May-2007 at 05:45. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #6  
Old 15-May-2007, 00:42
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 520
Peter_APIIT can only hope to improve
Thumbs up

Re: Vector Issues


Here is another program which simulate 2D array using vector.

I not really fully understand the program. Therefore, your help is greatly appreciated by me and others.

Below is my program:
CPP / C++ / C Code:
/* Vector of Vector which simulate 
   two-dimensional array

   Iterator is a generalization of pointer.
   Iterator is object pointer which pointed
   to any data type
*/

#include<iostream>
#include<vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::iterator;

int main(int argc, char *argv[])
{
	int outerloop, innerloop;
	/* declare a vector of vector which have
	   3 row and 2 column and initialized it 
	   to zero
   */
	vector< vector<int> >vec(3, vector<int>(3,0));
	
	for (outerloop = 0;outerloop < 3;outerloop++)
	{
		for (innerloop = 0;innerloop < 3;innerloop++)
		{
			cout << "Enter a " << "[" <<outerloop <<  "]" << "[" << innerloop << "]" << "integer : ";
			cin >>  vec[outerloop][innerloop];
		}
	}
	
	
	vector<vector<int>> :: iterator outer_loop;
	vector<int> :: iterator inner_loop;

	for (outer_loop=vec.begin();outer_loop!=vec.end();outer_loop++)
	{
		for (inner_loop=(*outer_loop).begin();inner_loop!=(*outer_loop).end();inner_loop++)
		{
			cout << "\nThe integer is " << *inner_loop;
		}// Why here must specified the *inner_loop 	
	}	 //	and not *outer_loop, *(outer_loop + *(inner_loop));
			
			
	return 0;
}

Thanks for your help.

Your help is greatly appreciated by me and others.
  #7  
Old 15-May-2007, 09:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Vector Issues


Quote:
Originally Posted by Peter_APIIT
Here is another program which simulate 2D array using vector.
.
.
.

If you are going to be using the "vector of vectors" as 2-D array, why not use array notation throughout? Understanding iterators is something to learn, but, in the meantime, why not use simpler notation:

CPP / C++ / C Code:
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    unsigned i, j;
    unsigned number_of_rows = 3;
    unsigned number_of_cols = 2;

    //
    // In the following statement, vec is declared to be a vector
    // of vectors of ints.
    // vec is initialized to have "number_of_rows" elements
    //
    // Each row is initialized with a vector that has "number_of_cols"
    // elements each of the elements of the row is initialized to zero
    //
    vector<vector<int> > vec(number_of_rows, vector<int>(number_of_cols, 0));

    cout << "The number of rows of vec is equal to vec.size()" << endl
         << "The number of columns of the 2-D vector is equal to" << endl
         << "the number of elements of vec[0], or vec[1], or ... " << endl
         << endl
         << "    vec.size()    = " << vec.size() << endl
         << "    vec[0].size() = " << vec[0].size() << endl << endl;

    for (i = 0; i < vec.size(); i++) {
        for (j = 0; j < vec[0].size(); j++) {
            cout << "Enter vec" << "[" << i << "]" << "[" <<
                j << "]: ";
            cin >> vec[i][j];
        }
    }


    for (i = 0; i < vec.size(); i++) {
        for (j = 0; j < vec[0].size(); j++) {
            cout << "vec[" << i << "][" << j << "] = "
                 << vec[i][j] << endl;
        }
    }
    return 0;
}

Here is a sample program run:

Code:
The number of rows of vec is equal to vec.size() The number of columns of the 2-D vector is equal to the number of elements of vec[0], or vec[1], or ... vec.size() = 3 vec[0].size() = 2 Enter vec[0][0]: 1 Enter vec[0][1]: 2 Enter vec[1][0]: 3 Enter vec[1][1]: 4 Enter vec[2][0]: 5 Enter vec[2][1]: 6 vec[0][0] = 1 vec[0][1] = 2 vec[1][0] = 3 vec[1][1] = 4 vec[2][0] = 5 vec[2][1] = 6


Regards,

Dave
  #8  
Old 16-May-2007, 05:31
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 520
Peter_APIIT can only hope to improve
Thumbs up

Re: Vector Issues


I have know array notation and pointer for 2D arrary. Due to this reasons, i would like to try out the new things.

Pointer Notation in C:
Code:
*(*(ptr + row_loop) + col_loop);

Thanks for your help.

Your help is greatly appreciated by me and others.
  #9  
Old 16-May-2007, 14:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Vector Issues


Quote:
Originally Posted by Peter_APIIT
I have know array notation and pointer for 2D arrary.

One of the most fundamental facts of C (and C++) pointer/array notation is the following:

If p is a pointer data type and m is an integer data type, then the following two notations are semantically identical. Not "almost the same", not "usually the same", not "depending on the context", but identical

This is a definition of the notation.

CPP / C++ / C Code:
p[m] is semantically identical to *(p + m)

Note that this does not (not) imply that "arrays are the same thing as pointers". It's just notation.

Now, in C (and C++) we don't really have 2-Dimensional arrays. We have arrays of arrays. However, since 2-Dimensional arrays are so useful, the array notation defined above also extends to more than one dimension.


Suppose pp is a "pointer to pointer" data type, and n is an integer data type.

Then the array notation defined above can be extended to the following definition:

CPP / C++ / C Code:
pp[m][n] is semantically identical to *(pp[m] + n)

But we know that
CPP / C++ / C Code:
pp[m] is semantically equal to *(pp + m)
Putting them together we have that
CPP / C++ / C Code:
pp[m][n] is semantically identical to *(*(pp+m) + n)

Regards,

Dave

Left as an exercise for the student: Would you like to try for 3-Dimensional array notation using a "pointer to pointer to pointer?"
  #10  
Old 18-May-2007, 01:34
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 520
Peter_APIIT can only hope to improve
Thumbs up

Re: Vector Issues


I think you have misunderstand what i want. Sorry to say so.

Basically, i have two question regarding the uses of vector and iterator.

Below is my problem :

1. Why the loop is not loop when i using push_back function.

CPP / C++ / C Code:
cout << "Enter a integer size : ";
    cin >> size;

vector<int>vec(size); // A vector has run-time size
    
    // Prompt user for input
    for (loop=0;loop<vec.size();loop++)
    {
        cout << "\nEnter integer : ";
        cin >> vec[loop];
//         vec.push_back(vec[loop]);
    }// The loop here is not stop even 
     // though is over the size 
     // How this can be solved


2. Why when i display the vector of vector, i need the column notation and not row notation. Sorry for my poor English.

CPP / C++ / C Code:
vector<vector<int>> :: iterator outer_loop;
    vector<int> :: iterator inner_loop;

    for (outer_loop=vec.begin();outer_loop!=vec.end();outer_loop++)
    {
        for (inner_loop=(*outer_loop).begin();inner_loop!=(*outer_loop).end();inner_loop++)
        {
            cout << "\nThe integer is " << *inner_loop;
        }// Why here must specified the *inner_loop     
    }     //    and not *outer_loop, *(outer_loop + *(inner_loop));    

I just post part of the code because this is a very simple program and to shorten the time you analyze the code.

I hope you are willing to help a stupid person.

Thanks for your help.

Your help is greatly appreciated by me and others.

GOD will blessed you all.
Last edited by admin II : 18-May-2007 at 15:38. Reason: Please surround your CPP code with [CPP] ... [/CPP]
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.