Vector concept in C++

Hi, I am trying to learn the vector concept in C++ and have written the following program with the help of an online tutorial:

#include
using namespace std;

int main()
{
int a[10], j, v1;
cout<<"Enter 10 numbers: ";
for (j=0; j<10; j++)
{
cin>>a[j];
}
vector v1 = a;
return v1;
}

However, its giving the following errors:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:progsvector.cpp" -o "C:progsvector.exe" -I"C:Dev-Cpplibgccmingw323.4.2include" -I"C:Dev-Cppincludec++3.4.2ackward" -I"C:Dev-Cppincludec++3.4.2mingw32" -I"C:Dev-Cppincludec++3.4.2" -I"C:Dev-Cppinclude" -L"C:Dev-Cpplib"
C:progsvector.cpp: In function `int main()':

C:progsvector.cpp:12: error: `vector' undeclared (first use this function)
C:progsvector.cpp:12: error: (Each undeclared identifier is reported only once for each function it appears in.)
C:progsvector.cpp:12: error: expected `;' before "v1"

Execution terminated


Please help me learn the vector concept!

Thanks in advance...


Abhishek.

Comments

  • [b][red]This message was edited by stober at 2006-10-1 2:50:8[/red][/b][hr]
    A vector is a template array that can hold almost any kind of data. When you create an instance of a vector you also have to tell it what kind of data it will hold. For example, if you want a vector of integers
    [code]
    #include
    #include

    using namespace std;

    int main()
    {
    [red] vector array;[/red]

    return 0;
    }
    [/code]

    Here is a short tutorial you might want to read

    http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/



  • : A vector is a template array that can hold almost any kind of data. When you create an instance of a vector you also have to tell it what kind of data it will hold. For example, if you want a vector of integers

    In math a vector can be a collection of numbers, or, as in physics, a magnitude with a direction. They are the same thing exept represented in different ways.

    Here's you should do it:
    [code]
    #include
    using namespace std;

    int main()
    {
    int a[10], j, v1[red][][/red];
    cout<<"Enter 10 numbers: ";
    for (j=0; j<10; j++)
    {
    cin>>a[j];
    }
    vector v1 = a;
    return v1;
    }
    [/code]
  • : Here's you should do it:
    [code]
    #include
    #include
    using namespace std;

    int main()
    {
    vector v1(10);
    cout<<"Enter 10 numbers: ";
    for (int j=0; j<10; j++)
    {
    cin>> v1[j];
    // flush '
    ' from the keyboard
    cin.ignore();
    }
    return 0;
    }
    [/code]
    :

    [blue]What he should do is rewrite the program. main() can not return a vector -- it always returns an in, normally 0.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories