- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Replace and Remove Elements in a Container in Java
Replace and Remove Elements in a Container Draw QR Code ISO/IEC18004 In Java Using Barcode printer for Java Control to generate, create QR image in Java applications. QR Code Reader In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. Key Ingredients
Barcode Drawer In Java Using Barcode generation for Java Control to generate, create barcode image in Java applications. Scan Barcode In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. Headers <algorithm> Classes Functions template <class ForIter, class T> ForIter remove(ForIter start, ForIter end, const T &val) template <class ForIter, class T> void replace(ForIter start, ForIter end, const T &old, const T &new) Make QR Code In Visual C#.NET Using Barcode creator for .NET framework Control to generate, create Quick Response Code image in .NET framework applications. QR Code JIS X 0510 Creator In .NET Framework Using Barcode generator for ASP.NET Control to generate, create QR image in ASP.NET applications. Herb Schildt's C++ Programming Cookbook
Quick Response Code Encoder In VS .NET Using Barcode printer for VS .NET Control to generate, create QR Code 2d barcode image in .NET framework applications. Creating QR-Code In VB.NET Using Barcode encoder for .NET Control to generate, create QR Code JIS X 0510 image in .NET framework applications. The STL provides functions that let you replace or remove elements At the core of this functionality is replace( ) and remove( ) Although both of these operations can be accomplished through the use of container-defined functions, in many cases, these algorithms streamline the task This recipe demonstrates them Data Matrix Generator In Java Using Barcode printer for Java Control to generate, create Data Matrix image in Java applications. Painting GS1 DataBar-14 In Java Using Barcode printer for Java Control to generate, create DataBar image in Java applications. Step-by-Step
Code 128 Code Set B Maker In Java Using Barcode generator for Java Control to generate, create ANSI/AIM Code 128 image in Java applications. Make Barcode In Java Using Barcode creator for Java Control to generate, create bar code image in Java applications. To remove or replace one or more elements in a sequence involves these steps: 1 To remove all elements that match a specified value, call remove( ), specifying the range to be modified and the value to remove 2 To replace all occurrences of elements that match a specified value, call replace( ), specifying the range to be modified, the value to replace, and the value to substitute Encoding ANSI/AIM Codabar In Java Using Barcode maker for Java Control to generate, create Rationalized Codabar image in Java applications. UPCA Maker In Java Using Barcode generator for BIRT Control to generate, create UPC Code image in Eclipse BIRT applications. Discussion
Print Bar Code In C# Using Barcode encoder for .NET Control to generate, create barcode image in VS .NET applications. UPC-A Supplement 5 Creator In Visual Studio .NET Using Barcode generator for Reporting Service Control to generate, create UPC Symbol image in Reporting Service applications. The remove( ) algorithm removes all occurrences of a specified element from a specified range It is shown here: template <class ForIter, class T> ForIter remove(ForIter start, ForIter end, const T &val) This algorithm removes all elements in the range start through end 1 that are equal to val It returns an iterator to the end of the remaining elements The order of the remaining elements is unchanged Within a specified range, the replace( ) algorithm replaces all occurrences of a specified element with another It is shown here: template <class ForIter, class T> void replace(ForIter start, ForIter end, const T &old, const T &new) Within the specified range start through end 1, replace( ) replaces elements that match the value old with elements that have the value new Generating UPC-A Supplement 2 In Visual Studio .NET Using Barcode creator for Visual Studio .NET Control to generate, create UPC A image in Visual Studio .NET applications. DataMatrix Decoder In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. NOTE The list container class provides its own implementation of remove( ) that is optimized for
USS Code 39 Recognizer In Visual Studio .NET Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. Create Data Matrix In None Using Barcode encoder for Online Control to generate, create DataMatrix image in Online applications. lists Therefore, when removing elements in a list, you should use that function rather than the remove( ) algorithm Example
The following example shows remove( ) and replace( ) in action: // Demonstrate remove() and replace() #include <iostream> #include <vector> #include <algorithm> using namespace std; template<class InIter> void show_range(const char *msg, InIter start, InIter end); 4: Algorithms, Function Objects, and Other STL Components
int main() { vector<char> v; vector<char>::iterator itr, itr_end; // Create a vector that contains A B C D E A B C D E for(int i=0; i<5; i++) { vpush_back('A'+i); } for(int i=0; i<5; i++) { vpush_back('A'+i); } show_range("Original contents of v:\n", vbegin(), vend()); cout << endl; // Remove all A's itr_end = remove(vbegin(), vend(), 'A'); show_range("v after removing all A's:\n", vbegin(), itr_end); cout << endl; // Replace B's with X's replace(vbegin(), vend(), 'B', 'X'); show_range("v after replacing B with X:\n", vbegin(), itr_end); cout << endl; return 0; } // Show a range of elements from a vector<char> template<class InIter> void show_range(const char *msg, InIter start, InIter end) { InIter itr; cout << msg; for(itr = start; itr != end; ++itr) cout << *itr << " "; cout << endl; } The output is shown here: Original contents of v: A B C D E A B C D E v after removing all A's: B C D E B C D E v after replacing B with X: X C D E X C D E Herb Schildt's C++ Programming Cookbook
Options and Alternatives
The STL provides several alternatives for removing and replacing elements Two that you will find especially useful are remove_copy( ) and replace_copy( ) Both generate a new sequence that contains the result of the operation Thus, the original sequence is unaltered The prototype for remove_copy( ) is shown here: template <class InIter, class OutIter, class T> OutIter remove_copy(InIter start, InIter end, OutIter result, const T &val) It copies elements from the specified range, removing those that are equal to val It puts the result into the sequence pointed to by result and returns an iterator to one past the end of the result The destination range must be large enough to hold the result The prototype for replace_copy( ) is shown next: template <class InIter, class OutIter, class T> OutIter replace_copy(InIter start, InIter end, OutIter result, const T &old, const T &new) It copies elements from the specified range, replacing elements equal to old with new It puts the result into the sequence pointed to by result and returns an iterator to one past the end of the result The destination range must be large enough to hold the result There are variations on remove( ), replace( ), remove_copy( ), and replace_copy( ) that allow you to specify a unary predicate that determines when an element should be removed or replaced These are called remove_if( ), replace_if( ), remove_copy_if( ), and replace_copy_if( ) Another algorithm that removes elements from a sequence is unique( ) It removes consecutive duplicate elements from a range It has the two forms shown here: template <class ForIter> ForIter unique(ForIter start, ForIter end) template <class ForIter, class BinPred> ForIter unique(ForIter start, ForIter end, BinPred pfn) Consecutive duplicate elements in the specified range are removed The second form allows you to specify a binary predicate that determines when one element is equal to another unique( ) returns an iterator to the end of the resulting range For example, assuming the preceding program, if v contains the sequence AABCCBDE, then after this statement executes itr_end = unique(vbegin(), vend()); the range vbegin( ) to itr_end will contain ABCBDE The STL also provides unique_copy( ), which works just like unique( ), except that the result is put into another sequence 4:
|
|