Forward_list__c++ of C + + container

Source: Internet
Author: User
Brief Introduction

Forward_list is a sequential container that can be inserted and removed at any point in a constant time. Forward_list is a one-way linked list.

The difference between forward_list and list is that the former is a one-way list, where the element has only one link to the next, and its iterator is forward effective; the latter is a two-way list, where there are two links, a link to the next element, and another link to the previous element, Its iterators are doubly efficient, and the list consumes more space in storage, and inserting and deleting elements consumes a little more time. The forward_list iterator is one-way, but it is more efficient than list.

Forward_list is more efficient at inserting, extracting, and moving elements at any point in the container than in other standard containers (array, deque, vector). However, several other containers are more efficient than the operations that directly access the elements based on the location.


member functions


Replication Control


forward_list::forward_list ()

Constructor: Creates a Forward_list container object that initializes its elements based on parameters. forward_list::~forward_list ()

destructor: Destroys a Forward_list container object. forward_list::operator=

Assignment function: Assigns a new content to the container instead of its current content. Sample Code

#include <iostream> #include <forward_list> using namespace std;
	void print (forward_list<int> l) {for (int &x:l) {cout << x << "";
} cout << Endl;
	
	} Template<class container> Container by_two (const container& x) {container temp (x);
	for (auto &x:temp) {x *= 2;
return to temp;
	int main (void) {//Forward_list::forward_list () forward_list<int>;
	Forward_list<int> second (5, 10);
	Forward_list<int> third (Second.begin (), Second.end ());
	Unlike other container forward_list<int> fourth (second);
	Forward_list<int> Fifth (move (fourth));	
	
	forward_list<int> Sixth = {2, 5, 9, 6};
	cout << "A:";

	Print (a);
	cout << "Second:";
	
	print (second);
	cout << "Third:";
	
	print (third);
	cout << "Fourth:";
	
	print (fourth);
	cout << "Fifth:";
	
	print (fifth);
	cout << "sixth:";
	
	Print (sixth); forward_list::operator= first = second;
	Second = By_two (a);
	cout << "A:";
	Print (a);
	print (second);
return 0;
 }

iterator


Forward_list::before_begin ()

Returns an iterator that points to the previous position of the first element in the Forwar_list container and returns a value type of iterator/const_iterator. Forward_list::begin ()

Returns an iterator that points to the first element of the Forward_list container and returns a value type of iterator/const_iterator. forward_list::end ()

Returns the next position of the last element of the container. The return value type is iterator/const_iterator. Forward_list:cbefore_begin ()

Returns a const_iterator iterator that points to the previous position of the first element in the Forwar_list container. Forward_list::cbegin ()

Returns a const_iterator iterator that points to the first element of the Forward_list container. forward_list::cend ()

         returns a const_iterator iterator that points to the next position in the last element of the Forward_list container. Sample Code

#include <iostream>
#include <forward_list>

using namespace std;

Template<class container>
void print (container& x)
{for
	(auto &value:x) {
		cout < < value << "";
	}
	cout << Endl;
}

int main (void)
{
	forward_list<int>-i = {8, n};
	
	Forward_list::before_begin ()
	First.insert_after (First.before_begin (), 5);
	Print (a);
	
	Forward_list::end () for
	(Forward_list<int>::iterator it = First.begin (); 
		It!= first.end (); ++it) {	
		cout << *it << "";
	}
	cout << Endl;
	
	Forward_list::cbefore_begin ()
	First.insert_after (First.cbefore_begin (), 2);
	Print (a);
	
	Forward_list::cbegin () cend () for
	(Forward_list<int>::const_iterator it = First.cbegin (); 
		It!= first.cend (); ++it) {
		cout << *it << "";
	}
	cout << Endl;
	
}
Capacity


Forward_list::empty ()

Test whether the Forward_list container is empty. return value type bool, False if NULL returns TRUE. forward_list::max_size ()

Returns the maximum number of elements that the Forward_list container can store. Sample Code

#include <iostream>
#include <forward_list>

using namespace std;

int main (void)
{
	forward_list<int>-i = {2, 5, 7, 9};
	
	Forward_list::empty ()
	cout << "is" << (First.empty ()?) Empty ":" Not empty! ") << Endl;
	
	Forward_list::max_size ()
	cout << "The max size is" << first.max_size () << Endl;
	
	return 0;
}

Element Access


Forward_list::front ()

Returns a reference to the first element of the Forward_list container. The return value type is reference/const_reference. Sample Programs

#include <iostream>
#include <forward_list>

using namespace std;

int main (void)
{
	forward_list<int>-i = {3, 4, 5};
	
	Froward_list::front ()
	forward_list<int>::reference re = First.front ();
	cout << "The" "The" the "" is "<< re << Endl;
	Re = 2;
	cout << "The" "The" the "" is "<< re << Endl;
	
	return 0;
}

modifiers


forward_list::assign ()

Assigns new content to the Forward_list container in place of its original content, and then modifies the container's size. Forward_list::emplace_front ()

Inserts an element before the first element of the Forward_list container, which is constructed by its own constructor. Typically used for composite type data operations. forward_list::p ush_front ()

Inserts an element before the first element of the Forward_list container. forward_list::p op_front ()

Delete the first element of the Forward_list container, size minus 1. Forward_list::emplace_after ()

Inserts an element after the specified position, constructed by its constructor. Typically used for composite type data operations. Forward_list::insert_after ()

Inserts one or a set of elements after the specified position. Forward_list::erase_after ()

Deletes one or a set of data at the specified location. Forward_list::swap ()

Swap the contents of two forward_list containers. forward_list::resize ()

Reset the size of the Forward_list container. If the Set value n is less than the current container's capacity, only the first n elements of the container are fetched, and if N is greater than the current container's capacity and given a specified fill value, the container is expanded to a capacity of N and populated with a fill value, otherwise populated with the default constructor value. forward_list::clear ()

Deletes all elements in the container forward_list. Sample Programs

#include <iostream> #include <forward_list> using namespace std;
	Template<class container> void Print (container x) {for (auto &value:x) {cout << value << "";
} cout << Endl;
	int main (void) {forward_list<int>;
	
	Forward_list<int> second;
	Forward_list::assign () first.assign (3, 5);
	Print (a);
	Second.assign (First.begin (), First.end ());
	print (second);
	First.assign ({2, 3, 4});
	
	Print (a);
	Forward_list::emplace_front () forward_list<pair<int, char>> list;
	List.emplace_front ("a");
	List.emplace_front (' B ');
	List.emplace_front (' C ');
	for (auto &x:list) {cout << "(" << x.first << "," << x.second << ")" << ";
	
	} cout << Endl;
	Forward_list::p Ush_front () First.push_front (1);
	
	Print (a);
	Forward_list::p Op_front () First.pop_front ();
	
	Print (a); Forward_list::emplace_after Forward_list<pair<int, Char>>::iterator it;
	it = List.before_begin ();
	List.emplace_after (it,, ' d ');
	for (auto &x:list) {cout << "(" << x.first << "," << x.second << ")" << ";
	
	} cout << Endl;
	Forward_list::insert_after () First.insert_after (First.before_begin (), 1);
	Print (a);
	First.insert_after (First.before_begin (), Second.begin (), Second.end ());
	Print (a);
	First.insert_after (First.before_begin (), 3, 6);
	
	Print (a);
	Forward_list::erase_after () First.erase_after (First.before_begin ());
	Print (a);
	First.erase_after (First.begin () + +, first.end ());
	
	Print (a);
	Forward_list::swap () First.swap (second);
	Print (a);
	
	print (second);
	Forward_lsit::resize () first.resize (2);
	Print (a);
	First.resize (5);
	Print (a);
	First.resize (10, 10);
	
	Print (a);
	Froward_list::clear () first.clear ();
	
	Print (a);
return 0;	
 }
Operations


Forward_list::splice_after ()

Moves one or a group of elements at a specified position in a forward_list container to a different forward_list container at the specified location and removes it from the current container. Forward_list::remove ()

Deletes all elements equal to the specified value in the container. forward_list::remove_if ()

Deletes all elements in the container that meet the specified criteria. Forward_list::unique ()

Deletes all elements in the container that are the same or satisfies the given condition, leaving only the first one. Before calling this function, it is necessary to sort the elements in the container first. Forward_list::merge ()

Merge two forward_list containers. The function is called before the container is ordered. Foreard_list::sort ()

The Rorward_ist container is sorted to change the location where the element is stored in the container. Foreard_list:reverse ()

Reverses the storage location of the elements in the container. Sample Programs

#include <iostream> #include <forward_list> using namespace std;
	Template<class container> void Print (container x) {for (auto &value:x) {cout << value << ';
} cout << Endl;

BOOL Condition (const int& value) {return (value > 5);}
	Class is_old_class{Public:bool operator () (const int& value) {return ((value% 2) = = 1);

}
};

BOOL Same_integral_part (double, double second) {return (int (a) = Int (second));}
	Class Is_near_class {Public:bool operator () (double, double second) {return (Second-first) < 5.0;

}
};
	int main (void) {forward_list<int>-i = {1, 2, 3};
	forward_list<int> second = {10, 20, 30};
	
	forward_list<double> third = {15.2, 73.0, 3.14, 15.85, 69.5, 73.0, 3.99, 15.2, 69.2, 18.5};
	Forward_list::splice_after Auto it = First.begin ();
	First.splice_after (First.before_begin (), second);
	Print (a);
	print (second); Second.splice_after (SECond.before_begin (), A, First.begin (), it);
	Print (a);
	print (second);
	First.splice_after (First.before_begin (), Second, Second.begin ());
	Print (a);
	
	print (second);
	Forward_list::remove () First.remove (30);
	
	Print (a);
	Forward_lsit::remove_if () first.remove_if (condition);
	Print (a);
	Is_old_class Is_old_object;
	First.remove_if (Is_old_object);
	
	Print (a);
	Forward_list::unique () Third.sort ();
	Third.unique ();
	print (third);
	Third.unique (Same_integral_part);
	print (third);
	Is_near_class Is_near_object;
	Third.unique (Is_near_object);
	
	print (third);
	Forward_list::merge () First.sort ();
	Second.sort ();
	First.merge (second);
	
	Print (a);
	Forward_list::reverse () Third.reverse ();	
print (third);
 }





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.