forward_list

Hello,
So the code below is what I need the forward_list option to work for. In the list there is a section for iteration, which I take it means the number of inputs/items that are to be put into the list. Is there a way to make a user generated integer the number used in that function? like how I can use mainfile.open(filename.c_str()); to call the actual string content to name the file created with it?


std::forward_list< _Tp, _Alloc >::forward_list ( size_type __n ) [inline, explicit]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 //Possible use forward_list to generate the list of time points
// Will Generate user input data of time points to create list number of inputs to link

  for (int i=1; i <= anum; i++, group++) {
  	outgroup = group/n;
  cout << "  <AddressEntry>"
		  "\r\n\n"
		  "    <AddressData>" << stdnum << "     Total:_______"
		  "\r\n\n"
		  "R" << i << ""
		  "\r\n\n"
		  << outgroup << " hr          Tare:_______</AddressData>"
		  "\r\n\n"
		  "<Name>"
		  "\r\n\n"
		  "<FirstName>" << stdnum << "       Total:_______</FirstName>"
		  "\r\n\n"
		  "<LastName></LastName>"
		  "\r\n\n";
  }
The number of inputs/items that are to be put into the list need not be specified before-hand.
We can create an empty std::forward_list<> and then insert elements into it as and when required.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <forward_list>
#include <iostream>
#include <algorithm>

int main()
{
    std::forward_list<int> lst ; // create an empty list

    lst.push_front(12) ; // insert 12 at the front
    lst.push_front(10) ; // insert 10 at the front
    lst.push_front(8) ;

    lst.insert_after( lst.begin(), 9 ) ; // insert 9 after 8

    // insert 11 after 10
    lst.insert_after( std::find( lst.begin(), lst.end(), 10 ), 11 ) ;

    // print the list
    for( int v : lst ) std::cout << v << ' ' ; // 8 9 10 11 12
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/9f58f5be5c5ef6d7
Fantastic!
Thank you.
Topic archived. No new replies allowed.