C++ की list
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Elements Add करना
Elements push_front और push_back से list के दोनों ends में add किए जा सकते हैं, या एक iterator इस्तेमाल करके बीच में कहीं भी insert किए जा सकते हैं, सब constant time में क्योंकि दूसरे elements को shift करने की ज़रूरत नहीं।
उदाहरण: Adding Elements
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Elements Remove करना
pop_front और pop_back पहला या आखिरी element remove करते हैं, जबकि erase एक दी गई iterator position पर एक element remove करता है, सब बिना किसी दूसरे element को shift करने की ज़रूरत के।
उदाहरण: Removing Elements
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक list के जरिए Iterate करना
एक list square-bracket indexing support नहीं करता क्योंकि यह memory में contiguously store नहीं है, इसलिए elements इसकी बजाय iterators या एक range-based for loop से access होते हैं।
उदाहरण: Iterating Through a list
// 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;
}
Login to try C/C++/Java/PHP code in the editor
list बनाम vector
list कहीं भी constant-time insertion और removal offer करता है, लेकिन indexing के बिना सिर्फ sequential access, जबकि vector [] से fast random access offer करता है लेकिन बीच में slower insertion या removal।
उदाहरण: list vs vector
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक
std::listपर[]इस्तेमाल करना, जो random access support नहीं करता; इसकी बजाय iterators इस्तेमाल करें। - एक list पर
std::sort(l.begin(), l.end())call करना, जो compile नहीं होता;l.sort()इस्तेमाल करें। - iterate करते हुए एक element erase करना और फिर invalidated iterator इस्तेमाल करना।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: