C + + face question (iii)

Source: Internet
Author: User
Tags function prototype mutex volatile

1 What is a function object? What's the effect?

2 types of STL iterators?

The five types of iterators are as follows:

(1) Input iterator: Read only, one pass
The predefined implementations for input iterators are only Istream_iterator and istreambuf_iterator, which are used to read from an input stream istream. An input iterator can only parse each element of its choice once, and they can only move forward. A specialized constructor defines the value beyond the end. Always, an input iterator can parse the results of a read operation (once for each value) and move forward.
(2) Output iterator: Write only, one pass
This complements the input iterator, but is a write operation rather than a read operation. The predefined implementations for output iterators are only Ostream_iterator and ostreambuf_iterator, which are used to ostream write data to an output stream, and there is a generally less-used raw_storage_iterator. They can only parse once for each value written, and move forward only. For an output iterator, the concept of ending with a value beyond the end is not used. In summary, an output iterator can parse the value of a write operation (only once for each value), and then move forward.
(3) Forward iterator: Read/write multiple times
The forward iterator contains the functions of both the input and output iterators, plus the position specified by an iterator can be parsed multiple times, so that a value can be read/written multiple times. As the name implies, forward iterators can only move forwards. There are no predefined iterators for the forward iterator.
(4) bidirectional iterator: operator--
Bidirectional iterators have all the functions of a forward iterator. It can also use the operator--operator to move backward one position at a time. The iterator returned by the list container is bidirectional.
(5) Random-access iterators: Similar to a pointer
A random-access iterator has all the functions of a bidirectional iterator, plus a pointer-all function (a pointer is a random-access iterator), except that there is no "empty (null)" iterator and a null pointer corresponding to it. Basically, a random-access iterator can do anything like a pointer, including using the operator operator[] to index, add a value to a pointer to move forward or backward, or use comparison operators to compare between iterators.

Iterator category

Description

Input iterators

Reads the element from the container. Input iterators can only read one element at a time and move forward, and input iterators support only one pass, and the same input iterator cannot traverse a sequence two times

Output iterators

Writes an element to the container. An output iterator can move forward only one element at a time. Output iterators support only one pass algorithm, and a unified output iterator cannot traverse a sequence two times

Forward iterators

Combines the functionality of an input iterator and an output iterator and retains its position in the container

Bidirectional iterators

Functions of combining forward iterators and reverse iterators, supporting multiple-pass algorithms

Random-Access iterators

Combining the functionality of a bidirectional iterator with the ability to directly access any element in the container, you can skip over any element forward and backward

Operation of the iterator:

Each iterator can perform operations that include the previous iterator in the table. The operation of an iterator is essentially implemented by overloading the operator, and what the iterator supports and what it can do is determined by the operator overloaded by the iterator.

Iterator type Type of operation Description

All iterators

p++

++p

Post-built self-increment iterators

Predecessor self-increment iterator s ' s

Input iterators

*p

P=p1

P==p1

P!=p1

Complex reference iterators, as Rvalue

Assigning an iterator to another iterator

Compare the equality of iterators

Comparing the inequality of iterators

Output iterators

*p

P=p1

Complex reference iterators, as Lvalue

Assigning an iterator to another iterator

Forward iterators

Provides all the functions of an input-output iterator

Bidirectional iterators

--p

p--

Pre-built-in self-reducing iterators

Post-offset self-reducing iterators

Random-Access iterators

P+=i

P-=i

P+i

P-i

P[i]

P<p1

P<=p1

P>p1

P>=p1

Increments the iterator I bit

Decrements the iterator I bit

Iterators after P-bit plus I-bit

The iterator after the P-bit minus I bit

Returns the element reference of the P-bit element deviating from the I bit

Returns true if the position of the iterator P is before P1, otherwise false

The position of P returns true at the front of the P1 or in the same position, otherwise it returns false

Such as

3 10 binary and 16 binary conversions

4 without if,while,do-while,for, print out all integers greater than 0 less than K. function prototype void printless (int k)

5 char (127<<1 + 1)

6 usage of Volitale/explict

7 struct A {

Char

Char

Short

Float

Double

};

struct B {

Char

Short

Float

Char

Double

};

sizeof (A) =

sizeof (B) =

8 Database Things

9 guaranteed only one instance of the application

method One : The mutex mutex, the kernel object, can be used for inter-process mutual exclusion, but at this time cannot get the instance window handle that has been started, therefore cannot activate the instance window that already started

#include"stdafx.h"#include<iostream>#include<windows.h>#include<wtypes.h>intMain () {BOOL bexist=FALSE; HANDLE Hmutex=:: CreateMutex (null,true,l"a unique name."); if(GetLastError () = =error_already_exists) {Bexist=TRUE; }    if(Hmutex) {:: ReleaseMutex (Hmutex); }    if(bexist) {return 0; }     while(1) {Std::cout<<"Hello World";    } getchar (); return 0;}

Method Two :
In general, the simplest way to make a program run only one instance is, of course, using FindWindow () to find the main window, and if the main window already exists, it certainly means that an instance has already been run. The code is as follows:
This method has the flaw, the window name changes can no longer find, FindWindow () parameters classname and caption more difficult to obtain.
HWND hwnd = FindWindow (NULL, TEXT ("SINGLEINSTANCEFW"));
if (IsWindow (hWnd))
{
:: MessageBox (NULL, TEXT ("There is already an instance running. "), TEXT (" note "), MB_OK);
:: ShowWindow (HWnd, sw_normal); Show
:: SetForegroundWindow (HWND); Activated
return FALSE;
}

Method Three :

Compared with the above two methods, this method avoids the disadvantage of the above two methods, by SetProp () to the main program window to set a special property, and then at startup to traverse all the windows, to find a window containing a property of the handle

。 "This additional window property should also be destroyed when the window is destroyed" the disadvantage of this method is that the code is a little more, as follows:

Declaring global property names and property values
TCHAR g_strkscoreappname[] = _t ("afx_ksinstall_cpp__12036f8b_8301_46e2_adc5_a14a44a85877__");
HANDLE G_hvalue = (HANDLE) 1022;

Defining the enumeration window callback function
BOOL CALLBACK Enumwndproc (HWND hwnd, LPARAM LPARAM)
{
TCHAR str[200] = {0};
:: GetWindowText (hwnd, STR, 200);
HANDLE h = Getprop (hwnd, g_strkscoreappname);
if (h = = G_hvalue)
{
* (hwnd*) LParam = HWND;
return FALSE;
}
return TRUE;
}

Before the main window is created
HWND Oldhwnd = NULL;
:: EnumWindows (Enumwndproc, (LPARAM) &oldhwnd); Enumerate all the running Windows
if (Oldhwnd! = NULL)
{
:: MessageBox (NULL, TEXT ("There is already an instance running. "), TEXT (" note "), MB_OK);

:: ShowWindow (Oldhwnd, sw_normal); Show
:: SetForegroundWindow (Oldhwnd); Activated
return FALSE;
}

After the main window is created, attach a property to the window
:: SetProp (M_hwnd, G_strkscoreappname, G_hvalue);

Remove the attached property when the main window exits
:: Removeprop (M_hwnd, g_strkscoreappname);

method Four :

The above method two and method San du has a disadvantage, do not know everyone found, that is dependent on the existence of the window, no window of the program how to do, using method one is possible, but the method is not very suitable for the immediate modification of the state, such as I want to provide options to the user, can immediately modify whether to allow multiple instances, Like KMP provides immediate modification of whether to allow multiple instances, using global variables is a good solution, the use of global shared variables is mainly implemented in the VC framework program through the compiler. Create a new section with the #pragma data_seg precompiled directive, which defines a variable using the volatile keyword and must be initialized. The volatile keyword specifies that a variable can be accessed by an external process. Finally, in order for the variable to function in the process mutex process, it is also set to a shared variable while allowing read and write access. This can be used to inform the compiler by #pragma comment precompiled directives. The following is a list of process mutex codes that use global variables:

#pragma data_seg ("Shared")
int volatile g_lappinstance = 0;
#pragma data_seg ()
#pragma COMMENT (linker, "/SECTION:SHARED,RWS")

if (0 = = g_lappinstance)
{
G_lappinstance = 1;
}
else if (1 = = g_lappinstance)
{
:: MessageBox (NULL, TEXT ("There is already an instance running. "), TEXT (" note "), MB_OK);
return FALSE;
}
Else
{
Direct start
}

10 How to execute a function before the main () function

11

C + + face question (iii)

Related Article

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.