← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 3 of 15

C++ की list

std::list STL का एक doubly-linked list container है, किसी भी position पर fast insertion और removal offer करते हुए, vector के मुकाबले slower random access की कीमत पर।
Syntax
cpp
#include <list>

std::list<data_type> list_name = {value1, value2};
list_name.push_back(value);
list_name.push_front(value);

std::list क्या है?

std::list Standard Template Library का एक container है जो एक doubly-linked list की तरह implemented है, जहाँ हर element अपने neighbors से linked है, sequence में कहीं भी fast insertion और removal offer करते हुए।

उदाहरण: What is std::list?

cpp
// Include std::cout and std::cin
#include <iostream>
#include <list>

// Program execution starts in main()
int main() {
	std::list<int> nums = {1, 2, 3};
	// Print to the console with cout
	std::cout << nums.front() << " " << nums.back() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Elements Add करना

Elements push_front और push_back से list के दोनों ends में add किए जा सकते हैं, या एक iterator इस्तेमाल करके बीच में कहीं भी insert किए जा सकते हैं, सब constant time में क्योंकि दूसरे elements को shift करने की ज़रूरत नहीं।

उदाहरण: Adding Elements

cpp
// Include std::cout and std::cin
#include <iostream>
#include <list>

// Program execution starts in main()
int main() {
	std::list<int> nums = {2, 3};
	nums.push_front(1);
	nums.push_back(4);
	for (int n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Elements Remove करना

pop_front और pop_back पहला या आखिरी element remove करते हैं, जबकि erase एक दी गई iterator position पर एक element remove करता है, सब बिना किसी दूसरे element को shift करने की ज़रूरत के।

उदाहरण: Removing Elements

cpp
// Include std::cout and std::cin
#include <iostream>
#include <list>

// Program execution starts in main()
int main() {
	std::list<int> nums = {1, 2, 3};
	nums.pop_front();
	nums.pop_back();
	for (int n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक list के जरिए Iterate करना

एक list square-bracket indexing support नहीं करता क्योंकि यह memory में contiguously store नहीं है, इसलिए elements इसकी बजाय iterators या एक range-based for loop से access होते हैं।

उदाहरण: Iterating Through a list

cpp
// Include std::cout and std::cin
#include <iostream>
#include <list>

// Program execution starts in main()
int main() {
	std::list<int> nums = {1, 2, 3};
	for (int n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

list बनाम vector

list कहीं भी constant-time insertion और removal offer करता है, लेकिन indexing के बिना सिर्फ sequential access, जबकि vector [] से fast random access offer करता है लेकिन बीच में slower insertion या removal।

उदाहरण: list vs vector

cpp
// Include std::cout and std::cin
#include <iostream>
#include <list>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named v (a resizable array)
	std::vector<int> v = {1, 2, 3};
	// Print to the console with cout
	std::cout << v[1] << std::endl;
	std::list<int> l = {1, 2, 3};
	l.push_front(0);
	// Print to the console with cout
	std::cout << l.front() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक std::list पर [] इस्तेमाल करना, जो random access support नहीं करता; इसकी बजाय iterators इस्तेमाल करें।
  2. एक list पर std::sort(l.begin(), l.end()) call करना, जो compile नहीं होता; l.sort() इस्तेमाल करें।
  3. iterate करते हुए एक element erase करना और फिर invalidated iterator इस्तेमाल करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.