range loop with pointer?

Hi

Trying to teach myself the range loop. Tried iterating through a pointer structure,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

int main () {

  struct item {
    std::string item_name;
    double price;
  };

  int item_count{0};
  std::cout <<"Enter number of items : ";
  std::cin >> item_count;

  item *ptr = new item[item_count];

  for ( item *p : ptr )
  {
    std::cout << "Enter item name : ";
    std::cin >> p->item_name;
    std::cout << "Enter Price    : ";
    std::cin >> p->price;
  }

  std::cout << "Third item : " << (ptr+2)->item_name << std::endl;
  std::cout << "Third price: " << (ptr+2)->price << std::endl;
  return 0;
}


The following compile error occurs:

range_loop2.cpp: In function ‘int main()’:
range_loop2.cpp:17:19: error: ‘begin’ was not declared in this scope
for ( item *p : ptr )

So question, is it not possible to use a range for loop over a dynamic structure ? Or, is there something wrong?
I do not think this is possible to use a for( : ), because there is no iterator for the compiler to automatically know how big the data structure that you are using is.

It does no matter too much because you already know how big your structure is if you use your item_count variable. so using a for( ; ; ) loop should work just the same.

thanks for the link. Useful reading, but not to that standard as of yet, still some way to go before I understand all that is mentioned :-)
You don't need to understand it all -- just have an idea of what it does.

Here's your code, with minor modifications:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iterator>
#include <string>

// from stackoverflow
namespace std {
        template <typename T> T* begin(std::pair<T*, T*> const& p)
        { return p.first; }
        template <typename T> T* end(std::pair<T*, T*> const& p)
        { return p.second; }
    }
    
// your item type (...is not likely to be local to one function)
struct item {
    std::string item_name;
    double price;
};
  
int main () {

  int item_count{0};
  std::cout <<"Enter number of items : ";
  std::cin >> item_count;

  item *ptr = new item[item_count];

  // you need this...
  using std::begin;
  using std::end;

  // the modified ranged-for (notice the reference type)
  for ( item& p : std::make_pair(ptr,ptr+item_count) )
  {
    std::cout << "Enter item name : ";
    std::cin >> p.item_name;
    std::cout << "Enter Price    : ";
    std::cin >> p.price;
  }

  // (make sure you have enough elements before trying to access one...)
  if (item_count >= 3)
  {
    // (notice that you can access it with standard array syntax)
    std::cout << "Third item : " << ptr[2].item_name << std::endl;
    std::cout << "Third price: " << ptr[2].price << std::endl;
  }
  return 0;
}

Hope this helps.

Hi,
great, thanks for the code.
Makes more sense now!
Last edited on
Topic archived. No new replies allowed.