C ++VC ++ programming troubleshooting and answers

zhaozj2021-02-16  129

Difficult Questions and Answers C / VC programming Author: MH Li himself with C / when VC programming, some of the more difficult to solve the problems encountered, after Best obtaining the solution, summed up some solutions, I hope these experiences Talking about everyone can help.

VC problem, the wrap within the edit box; the problem of the C problem standard library, can the List's iterator can move random? What kind of container should be chosen when the C problem standard library is in the case of a lot of deletions? 1, [VC Question] In the Edit box? A: Suppose you want to output "12345 / N6789" in the edit box. First, set the multiline property of the edit box to True. In addition to add / N, there is / R in addition to add / n. That is, if 12345 and 6789 are displayed in two lines, 12345 / R / N6789 is required. The code is as follows: m_strdata = "12345 / n67890"; // m_strdata is the variable associated with the edit box

Updatedata (false); // update control

2, [C standard library problem] Can the iterator of the List move? A: Since the internal implementation of the List is a two-way linked list, the list is required to move backward (or backward) in turn, then move one position in turn, so the list is only defined and - operation Instead, not defined , -, = and - = and other operators. So I want to move a distance from the iterator, I need my own programming and use a small loop. The code is as follows: #include

Using namespace std;

List

MYLIST;

... // MyList initialization and other operations

List

:: const_iterator itlist = mylist.begin ();

// itList moves the LEN distance forward

For (int i = 0; i

{

itList;

}

... // Other operations

3, [C standard library problem] What kind of container should be chosen in the case of deleting a lot? A: According to the standard library, when the delete / insert occurs only in the tail of the container, you should use the Vector; when the deletion / insert occurs only at the first / tail of the container, you should use a demue; when the deletion / insertion is more operation, And the deletion / insert is not the end of the container, and the list should be selected. However, there is also a problem here. When using the list, since its iterator can only move one position once, there is a large number of deleted operations, there is a large number of iterator mobile operations, so the efficiency of using List will be low; One disadvantage is that the occupancy space is larger than the same capacity size, which is due to the realization of the List, which is more than two pointers (two-way linked list) than the elements in Vector, so every element Small (several bytes), and the number of elements is very wasting space when using List. If you use a vector, delete an intermediate element will result in a lot of element movement, and it is not high. In order to solve this problem, we can use this: Use the Vector container, but not directly delete the elements, but apply for a temporary vector, will Originally, the useful elements that have not been removed in the vector sequentially add / save to the temporary vector, and then replace the original vector with this temporary vector. If you implement the number of elements you want to delete, then you can reserve the space of the temporary vector, will be useful The elements save to the temporary vector, the code is as follows: #includeusing namespace std;

Vector

MYVEC;

... // MyVEC initialization and other operations

Vector

TempVec;

/ / Delete NUM elements in MyVec

TempVec.Rserve (MyVec.size () - NUM); // Reserved Space for Temporary VECTOR

For (;;)

{

Save the useful elements in MyVec into TEMPVEC;

}

MyVec.swap (TEMPVEC); // myVec capacity is equal to or slightly larger than MyVec.Size ()

... // Other operations

This is just a few questions I have summarized, and I have solved the problem in the future, and I will always summarize.

Latest Reviews [Published Reviews] [Article Submission] See all comments Recommend to friends print

转载请注明原文地址:https://www.9cbs.com/read-8552.html

New Post(0)