Difficult problems and solutions of c++/vc++ programming

Source: Internet
Author: User
Tags requires

I am in the use of c++/vc++ programming, encountered some of the more difficult to solve the problem, after the solution, summed up a number of solutions, I hope these experience can be helpful to everyone.

1, [VC question] Edit the line in the box?

A: Suppose you want to output "12345\n6789" in the edit box. The first time you want to set the Multiline property of the edit box to True, in addition to the output, there is \ r. That is, if you want 12345 and 6789 to be displayed in two rows, you need to output 12345\r\n6789. The code is as follows:

m_strData = "12345\r\n67890"; // m_strData是和编辑框关联的变量
UpdateData(FALSE); //更新控件

2, [C + + standard library of problems] List of the iterator can be randomly moved?

Answer: Because the list's internal implementation is a two-way linked list, the list requires that the iterator (pointer) move from front to back (or forward) in turn, moving one position at a time, so that the list defines the + + and-operator, and does not define + 、-、 + = and-= operators. So to the list of the iterator to move a distance, you need to programmatically implement, with a small loop on the line, the code is as follows:

#include <list>
using namespace std;
list myList;
… // myList的初始化及其他操作
list::const_iterator itList = myList.begin();
// itList向前移动len个距离
for ( int i= 0; i < len; i++ )
{
  ++itList;
}
… // 其他操作

3, [C + + standard library of problems] in the deletion of many cases, what kind of container should be selected?

A: According to the standard library instructions, vector should be used when the delete/insert occurs only at the end of the container, deque should be used when the delete/insert occurs only at the top/tail of the container, when the delete/insert operation is large, and the delete/insert is not just the end of the container, then the list should be selected.

But there's also a problem here, when using the list, because its iterator can only move one position at a time, a large number of iterator moves are required when there is a large number of deletions, so it is inefficient to use the list; another disadvantage of the list is that it takes up more space than a vector of the same size, This is because the implementation of the list requires pointers, and each element is more than two pointers to the elements in the vector (doubly linked lists), so when each element is small (several bytes) and the number of elements is large, using the list is a waste of space.

If vector is used, then removing the intermediate element can result in a lot of element movement and inefficiency, and in order to solve this problem we can do this by using the vector container, but not deleting the element directly, but applying for a temporary vector, Add/Save the unused elements in the original vector to the temporary vector, then replace the vector with this temporary vector, and if the implementation knows the number of elements to be deleted, you can reserve the space for the temporary vector in advance, Then save the useful elements in a temporary vector with the following code:

#include <vector>
using namespace std;
vector myVec;
… // myVec的初始化以及其它操作
vector tempVec;
          // 删除myVec中的num个元素
tempVec.reserve( myVec.size() - num ); // 预留临时vector的空间
for ( ;; )
{
  将myVec中的有用元素存到tempVec中;
}
myVec.swap( tempVec ); // myVec的容量等于或稍大于myVec.size()
… // 其它操作

This is just my current summary of a few problems, the future encountered problems at any time to solve, summarized.

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.