STL查找时比较函数对象的实现

29 3 月, 2007

我们在使用STL的vector或list过程中经常要用到查找操作,对于需要排序的操作则需要找到插入点。如果我们放到这些容器中的对象是我们自己定义的话则需要自己实现比较函数。

我们这里举两个例子,分别是未排序的find_if和排好序的lower_bound:

假设我们有这样一个类:
class CUser
{
public:
 CUser(const std::string& strUserName);

 std::string& GetUserName()
 {
  return m_strUserName;
 }

private:
 std::string m_strUserName;  // User name
};
typedef vector<CUser*> TUser;
typedef vector<CUser*>::iterator TUserIt;

定义一个vector类:
TUser vctUser;

1. 我们想从无序vector中找到某一个用户的对象,可以这样做:
CUser* pUser = new CUser(string("lordong"));
TUserIt itFind = find_if(vctUser.begin(), vctUser.end(), is_needed_user<CUser*>(pUser));
if (itFind != vctUser.end())
{
 // 找到
}
// 决定是否需要释放pUser的资源

is_needed_user函数对象的实现代码:
template <typename PType>
class is_needed_user : std::unary_function<PType, bool>
{
public:
 is_needed_user(const PType pArg) : m_pValue(pArg) { }
 ~is_needed_user() { }

 bool operator()(const PType p) const
 {
  return _tcsicmp(m_pValue->GetUserName().c_str(), p->GetUserName().c_str()) == 0;
 }

private:
 PType m_pValue;
};

2. 我们想从有序的vector中找到某一对象,可以这样做:
CUser* pUser = new CUser(string("lordong"));
TUserIt itFind = lower_bound(vctUser.begin(), vctUser.end(), CompareUser<CUser*>());
if (itFind != vctUser.end())
{
 // 参见binary_search函数,注意参数顺序
 if (!CompareUser<CUser*>()(pUser, *itFound))
 {
  // 找到
 }
 else
 {
  // 找到插入点
 }
}
// 决定是否需要释放pUser的资源

CompareUser函数对象的实现代码:
template<typename Ty>
struct CompareUser : public std::binary_function<Ty, Ty, bool>
{
 bool operator()(const Ty& first, const Ty& second)
 {
  return _tcsicmp(first->GetUserName().c_str(), second->GetUserName().c_str()) < 0;
 }
};

 

使用COM组件创建快捷方式的实现

26 3 月, 2007

从网上搜罗得到一个创建快捷方式的解决方案,对其进行整理并写成可独立使用的函数:

// Create shortcut
// @param lpszSource LPCTSTR Source file name or file path
// @param lpszShortcut LPCTSTR Shortcut name, e.g. Notepad.lnk
BOOL CreateShortcut(LPCTSTR lpszSource, LPCTSTR lpszShortcut)
{
 HRESULT hr;
 IShellLink* psl = NULL;
 IPersistFile* ppf = NULL;
 WORD wsz[MAX_PATH];
 BOOL bInitCom = FALSE;
 BOOL bRet = FALSE;

 do {
  hr = CoCreateInstance(CLSID_ShellLink, NULL ,CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl);
  if (!bInitCom && FAILED(hr))
  {
   bInitCom = TRUE;
   hr = CoInitialize(NULL);
  }
  else
  {
   break;
  }
 } while(SUCCEEDED(hr));
 
 if(SUCCEEDED(hr))
 {
  psl->SetPath(lpszSource);
  hr = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
  if(SUCCEEDED(hr))
  {
   MultiByteToWideChar(CP_ACP, 0, lpszShortcut, -1, wsz, MAX_PATH);
   bRet = SUCCEEDED(ppf->Save(wsz, STGM_READWRITE));

   ppf->Release();
  }
  psl->Release();
 }
 
 if (bInitCom)
 {
  CoUninitialize();
 }

 return bRet;
}

google 服务器ip地址列表

21 3 月, 2007

america.google.com
IP Address #1:64.233.183.104
IP Address #2:64.233.183.147
IP Address #3:64.233.183.99
IP Address #4:64.233.183.103

asia.google.com
IP Address #1:66.102.7.99
IP Address #2:66.102.7.147
IP Address #3:66.102.7.104

base.google.com
IP Address #1:66.249.93.104
IP Address #2:66.249.93.99

book.google.com
IP Address #1:72.14.203.133

calendar.google.com
IP Address #1:66.102.11.99
IP Address #2:66.102.11.104

code.google.com
IP Address #1:66.102.11.104
IP Address #2:66.102.11.99

directory.google.com
IP Address #1:64.233.183.147
IP Address #2:64.233.183.99
IP Address #3:64.233.183.103
IP Address #4:64.233.183.104

download.google.com
IP Address #1:66.102.11.99
IP Address #2:66.102.11.104

earth.google.com
IP Address #1:64.233.183.147
IP Address #2:64.233.183.103
IP Address #3:64.233.183.99
IP Address #4:64.233.183.104

email.google.com
IP Address #1:66.249.91.107

europe.google.com
IP Address #1:64.233.183.99
IP Address #2:64.233.183.103
IP Address #3:64.233.183.147
IP Address #4:64.233.183.104

images.google.com
IP Address #1:66.102.9.147
IP Address #2:66.102.9.99
IP Address #3:66.102.9.104

mail.google.com
IP Address #1:72.14.205.83
IP Address #2:72.14.205.19

map.google.com
IP Address #1:66.102.11.104
IP Address #2:66.102.11.99

news.google.com
IP Address #1:64.233.183.99
IP Address #2:64.233.183.103
IP Address #3:64.233.183.147
IP Address #4:64.233.183.104

talk.google.com
IP Address #1:66.102.11.125

tools.google.com
IP Address #1:216.239.59.104
IP Address #2:216.239.59.99
IP Address #3:216.239.59.103
IP Address #4:216.239.59.147

translate.google.com
IP Address #1:66.102.11.99
IP Address #2:66.102.11.104

video.google.com
IP Address #1:66.249.93.99
IP Address #2:66.249.93.104

www.google.com
IP Address #1:66.102.9.147
IP Address #2:66.102.9.99
IP Address #3:66.102.9.104

引用来源:http://blog.goldtimes.net/blogview.asp?logID=192

应该找个时间把我们的首页设计一下了!

20 3 月, 2007

最近打毛线,打得有点受不了了,公司情况也不太好,业务也不怎么好做。什么时候也得抽空做做我们的的网站首页了,老是懒啊 …

宝贝计划视频推荐

19 3 月, 2007

今晚9时在切换电视频道时被CETV-1台的《亲亲宝贝》之《思想之光》所吸引,被片中那种任由孩子自由发挥的方法所感触,该法很值得学习。

宝贝能在一大堆玩具中摸索,逐步了解各个玩具的用处。

宝贝能通过不断的按播放机上的按钮根据机器的反应熟悉播放机的使用。

宝贝能自己思考通过各种途径拿到自己想到的东西。

……

个人感觉该片对宝贝的启发教育很有帮助,在此记录下来,准备在空余的时候去找找,BT下载或购买都行。

这几天

14 3 月, 2007

38买了毛线回来,现在织得手发软,有点不想织了,但也不好不织,以后不干这样的事情了,真是累人。

当备案时收不到手机短信…

13 3 月, 2007
在ICP备案时收不到手机上的验证码短信是够让人迷茫的(我曾经为此专门和LP试发短信,钱是白花了)。
在网上搜索到解决方案,略为指点一下: 
 
1. 打开http://www.miibeian.gov.cn网页。
2. 输入登陆信息登录。
3. 当出现需输入"手机验证码"和"邮箱验证码"时,把址址栏由http://www.miibeian.gov.cn/loginSys.jsp改为http://www.miibeian.gov.cn/index_icp.jsp进入系统继续。
为了确保能收到备案的相关进度信息,请确保邮箱能收到验证码。

今天三月八号!

8 3 月, 2007

       今天3月8号,国际妇女节日,中午吃完午饭,公司领导商量下午放假。KAKA放假真爽,公司全体女人一起集体逛街,(全公司女人也就4个人)呵呵,逛街我喜欢。

       1点钟,女人们出发了,坐申陆线到人民广场,我们第一站莱福士,天啊,今天街上女人真多啊,到处都是成群结队的女人,呵呵,今天是女人“放风”的节日,开心的慰劳一下自己啦,昨天老公给我买了个手机,今天不敢花钱 了,好好陪同事饱眼福吧,呵呵,也开心。潘姐很会织毛衣,他们打算去买线织什么买背心,听说很漂亮,他们说了半天我也不晓得他们说的背心是什么样子,相信他们吧,也买点毛线回去呵呵,跟他们一样织,也开心,他们也各有收获。线买好了,于是又坐地铁到东昌路买线针,呵呵,各人都买好了线针,另2个同事有事情就回去了,我有陪段姐逛东昌小店,这里的小店真多,我们这家进去哪家,一会逛到希霞路上的一个小店,段姐试毛衣,呵呵穿了几件,看上一件蝙蝠衫,真的很漂亮,于是买回家去,呵呵,女人们开心的回家了!!!

     想想女人真容易满足哈,开心就好!咔咔,开始做饭了

今天是妇女节,祝老婆大人节日快乐!

8 3 月, 2007
妇女节从啥时候开始,咱不得而知,不过知道这天是大老爷们最头疼的事,得花钱吧,家务得自己干吧,哎真希望也有个妇男节,让咱大老爷们争口气呀。
听听咱家那口子啥说的:
歪歪袁: 大家都跟我说节日快乐,你怎么不祝我节日快乐啊
呵呵
LorDong: 谁说了来着
歪歪袁: 李小强都跟我说了的
呵呵
LorDong: 你是“妇女”吗,嘎嘎
歪歪袁: 还有很多同学啊,群啊,都说了,邻居啊
LorDong: 你老公怎么能跟别人一样呢
歪歪袁: 呵呵
那你要怎么个语众不同啊
LorDong: 不说呀
歪歪袁: 切
呵呵,该吃饭了,希望下午我们放假
LorDong: 嗯好
歪歪袁: 咔咔,我有新手机了,中午回去拿
看,要不是昨天给她买了个新手机,今天可就惨了……

用SetForegroundWindow将窗体提到最前的实现

8 3 月, 2007

前段时间写的C#程序启动欢迎窗体实现,在运行时虽然能显示欢迎窗体,但运行后不能把主窗体提到最前。结合网上搜罗了好多资料,自己做了好几个方案来实现,最终可以解决了。

起因:
2000/XP 改变了SetForegroundWindow的执行方式,不允许随便把窗口提前,打扰用户的工作。

思路:
可以用附加本线程到最前面窗口的线程,从而欺骗windows。

实现:
原理:将显示欢迎窗体的线程作为Foreground的线程。
1. API函数的导入
public const int SW_HIDE = 0;
public const int SW_NORMAL = 1;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMAXIMIZED = 3;

[DllImport(“User32.dll”)]
public static extern void SetForegroundWindow(IntPtr hwnd);

[DllImport(“User32.dll”)]
public static extern IntPtr GetForegroundWindow();

[DllImport(“User32.dll”)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, IntPtr pProcId);

[DllImport(“User32.dll”)]
public static extern bool AttachThreadInput(int idAttach, int idAttachTo, int fAttach);

[DllImport(“User32.dll”)]
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

2. 使用API函数实现
private static void ActivateMainInstance(IntPtr mainWindowHandle, int foreId)
{
int curId = GetWindowThreadProcessId(mainWindowHandle, IntPtr.Zero);

if (curId != foreId)
{
AttachThreadInput(foreId, curId, 1);
SetForegroundWindow(mainWindowHandle);
AttachThreadInput(foreId, curId, 0);
}
else
{
SetForegroundWindow(mainWindowHandle);
}
ShowWindowAsync(mainWindowHandle, SW_SHOWMAXIMIZED);
}

3. 调用:
private static ApplicationContext _context = null;
private static SplashScreen _splash = null;
private static MainFrame _mForm = null;

private static void OnAppIdle(object sender, EventArgs e)
{
if (_context.MainForm == null)
{
Application.Idle -= new EventHandler(OnAppIdle);

// Initialize main frame and show
_mForm.InitializeFrame(); // 花时间点
_context.MainForm = _mForm;
_context.MainForm.Show();

// Active main form
ActivateMainInstance(_mForm.Handle, _splash.ThreadId);

// Close splash screen
_splash.Close();
_splash = null;
}
}

4. Main函数中:
// Show splash screen
_splash = new SplashScreen();
_splash.Run();

// Application running
_mForm = new MainFrame();
_context = new ApplicationContext();
Application.Idle += new EventHandler(OnAppIdle);
Application.Run(_context);

5. SplashScreen的实现:
public class SplashScreen
{
private SplashForm _sForm = null;
private Thread _workThread = null;

///

/// Thread function: shows this splash form.
///

private void Show()
{
_sForm = new SplashForm();
_sForm.ShowDialog();
}

///

/// Thread id property
///

public int ThreadId
{
get
{
return _workThread.ManagedThreadId;
}
}

///

/// Runs this instance.
///

public void Run()
{
_workThread = new Thread(new ThreadStart(Show));
_workThread.Start();
}

///

/// Closes this instance.
///

public void Close()
{
if (_sForm != null)
{
_sForm.HideSplash = true;
_workThread.Join();
_workThread = null;
}
}
}