3

i'm trying to use algorithm lib & vector lib to first copy a set of numbers from an array into a vector then printing it using iteration, where is the problem of my code?

and one thing is that i chose 2 way to do this iteration first using vec.begin() ; vec.end() method & the other one is for (i = 0 ; i < vec.capacity() ; i++) both facing errors.

what should i do?

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int intArray[] = {5,6,8,3,40,36,98,29,75};

    vector<int> vecList(9);
    //vector<int>::iterator it;
    copy (intArray, intArray+9,vecList);
    //for(it  = vecList.begin() ; it != vecList.end() ; it++)
    for (int it = 0 ; it < vecList.capacity() ; it++)
    {
        cout<<*it<<endl;
     }

    system("pause");
    return 0;

}
TemplateRex
  • 69,038
  • 19
  • 164
  • 304

5 Answers5

7

There are several improvements possible.

You confuse iterators with indices. An iterator it is a glorified pointer into the vector, that you need to derefence by typing *it. An index i is an offset from the beginning of the vector and saying vecList[i] will give you that element.

The initialization of the vector is best done using initializer lists (C++11), rather than reading from an array.

You need to loop to vecList.size(). The capacity of the vector is the size of the allocated storage space for the elements of the vector container. Looping is best done with a ranged-for loop as shown by Kerrek SB, or a std::for_each + a lambda expression, or a regular for loop as you did. In that case however, it's best to get into the habit of doing it != vecList.end() (instead of using <) and doing ++it instead of it++.

Note that I also used auto to avoid writing the explicit iterator type. It's also a good habit to get into using auto wherever you can.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    // initialize vector with a list of elements
    vector<int> vecList {5,6,8,3,40,36,98,29,75}; 

    // loop from begin() to end() of vector, doing ++it instead of it++ 
    for (auto it = vecList.begin(); it != vecList.end(); ++it) 
    {
        cout<<*it<<endl;
    }

    // the pause command is better done by setting a breakpoint in a debugger

    return 0;

}

Output on Ideone (this uses the g++ 4.5.1 compiler, it's best to upgrade to at least that version to take advantage of C++11 features).

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • There's **a lot** more problems with the code. He's attempting to dereference an `int`, he's not initializing the index... – Luchian Grigore Aug 16 '12 at 06:53
  • @user1602447 As you can see from the output on Ideone, this code compiles on g++ 4.5.1 and later. If you are serious about learning C++, then it's best to upgrade to at least that (and preferably to the latest 4.7.1) version. – TemplateRex Aug 16 '12 at 07:14
  • 1
    I don't agree to using `auto` wherever you can. I use it when it removes unnecessary clutter. – fredoverflow Aug 16 '12 at 07:16
  • as i compile it these are the errors i get, and here's another question i'm using backtrack 5 r3 released 2 days ago it shoul have the latest version, nevermind, this one is using Qt C++ Plain text Project, it's The Latest version What about it? – T0M XeOn LuCiFeR Aug 16 '12 at 07:17
  • @FredOverflow Herb Sutter and Scott Meyers disagree with you. – TemplateRex Aug 16 '12 at 07:21
  • yes, read the first line in the errors: you need to compile with `-std=c++0x` – TemplateRex Aug 16 '12 at 07:23
  • GUYS...Hey please, What The Hell Should I Do...? It's Making Me More Confused Than OK-to_go... – T0M XeOn LuCiFeR Aug 16 '12 at 07:24
  • @rhalbersma : well how? using Qt i donno how...! – T0M XeOn LuCiFeR Aug 16 '12 at 07:25
  • @user1602447 If you want to know how to configure QtCreator to using different compile flags, please search this site for answers, or ask a new question about it. – TemplateRex Aug 16 '12 at 07:26
6

The problem is that you're confusing indexes and iterators.

w/ index:

 for (int i = 0  ; i < vecList.size() ; it++)
 {
    cout<<vecList[i]<<endl;
 }

w/ iterators

 for (std::vector<int>::const_iterator it = vecList.begin()  ; i != vecList.end() ; it++)
 {
    cout<<*it<<endl;
 }
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

A. you need to iterate on vecList.size() not vecList.capacity() which mean how much memory the vector is reserving for himself (not how much of it is in use).
B. you tried to use the integer index it as an iterator with the call to *it, you should check Luchian Grigore answer for the right way to do it.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
1

This isn't an answer, but I wanted to show how modern C++ allows you to do away with lots of the brittle dependencies on details:

int intArray[] = {5,6,8,3,40,36,98,29,75};

std::vector<int> vecList(std::begin(intArray), std::end(intArray));

for (int i : vecList) { std::cout << i << std::endl; }

Using iterators and algorithms idiomatically, you can often remove any explicit mention of details such as lengths of arrays, thus making your code more robust.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 2
    You're using C++11 anyway, so why not combine the first two lines? `std::vector vec {1, 2, 3};` Technically in this case, even `for (int i : {1, 2, 3}) std::cout << i << '\n';` is possible. – chris Aug 16 '12 at 07:01
  • @chris How about `for (int i : {1, 2, 3}) std::cout << i << '\n';`? ;) – fredoverflow Aug 16 '12 at 07:17
1

Typo mistake use : copy (intArray, intArray+9,vecList.begin());

so,

#include<iostream>
#include<vector>
#include <algorithm>

using namespace std;


int main()

{

int intArray[] = {5,6,8,3,40,36,98,29,75};

vector<int> vecList(9);
vector<int>:: iterator it;
copy (intArray, intArray+9,vecList.begin());
for (it=vecList.begin();it!=vecList.end(); it++)
{
    cout<<*it<<endl;
 }

system("pause");
return 0;

}
user1590595
  • 795
  • 2
  • 13
  • 37