在MFC中使用C++的string,要先#include
- void CStrnewView::OnDraw(CDC* pDC)
- {
- CStrnewDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- std::string s0("string mfc test");
- std::string s2("");
-
- pDC->TextOut(20, 20, s0.c_str());
-
- char chs[]="Hello";
- std::string s1(chs);
- pDC->TextOut(20, 50, s1.c_str());
- s1 = "哈哈哈哈!";
- pDC->TextOut(20, 80, s1.c_str());
-
- s1.assign("another string");
- pDC->TextOut(20, 110, s1.c_str());
- s1.assign("looking me",6);
- pDC->TextOut(20, 140, s1.c_str());
-
- s2.append("来自海外,");
- pDC->TextOut(20, 170, s2.c_str());
-
- s2.insert(0, "我");
- pDC->TextOut(20, 200, s2.c_str());
- }
直接赋值;使用char []初始化一个string对象;使用assign()指定一个新的内容