C++ 代码片段

1、

//条款1:迭代器
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
	vector<int> e;
	copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(e));
	vector<int>::iterator F1 = find(e.begin(), e.end(), 3);
	vector<int>::iterator F2 = find(e.begin(), e.end(), 5);
	*F2 = 21;                                                              //error 可能=e.end;
	copy(F1, F2, ostream_iterator<int>(cout, "\n"));    //error 不一定是有效的范围                            
	e.insert(--e.end(), 19);                                           //error,C++不允许对内置类型的临时变量进行修改,先减减有个临时变量
	copy(F1, F2, ostream_iterator<int>(cout, "\n"));    //error 迭代器已失效
	return 0;
}

  

 

2、MFC中点出对话框显示另存为的一个路径和文件:

void CPatchDlg::OnBnClickedBtnPath()
{
//一下为选择另存为一个路径
	m_strSavepath = "";
	BROWSEINFO bi;
	ZeroMemory(&bi, sizeof(BROWSEINFO));
	bi.hwndOwner = m_hWnd;
	bi.ulFlags   = BIF_RETURNONLYFSDIRS;
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
	BOOL bRet = FALSE;
	TCHAR szFolder[MAX_PATH*2];
	szFolder[0] = _T('\0');
	if (pidl)
	{
		if (SHGetPathFromIDList(pidl, szFolder))
			bRet = TRUE;
		IMalloc *pMalloc = NULL;
		if (SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc)
		{
			pMalloc->Free(pidl);
			pMalloc->Release();
		}
	}
	m_strSavepath = szFolder;//选择的文件夹路径
	UpdateData( FALSE ) ;
//以下四行表示选择一个文件 返回文件名和路径;
	//CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
	//if(dlg.DoModal()==IDOK)
	//	m_strSavepath=dlg.GetPathName();
	//UpdateData( FALSE ) ;
}



 

posted @ 2014-07-09 16:06  Kiveen  阅读(313)  评论(0编辑  收藏  举报