标签: cstring

如何连接多个CString

所有函数都返回 CString,这是 MFC 代码,必须以 32 和 64 位进行编译。

目前我正在使用

CString sURI = GetURL();
sURI += GetMethod();
sURI += "?";
sURI += GetParameters();
Run Code Online (Sandbox Code Playgroud)

存在任何方式可以执行相同操作,例如:

CString sURI = GetURL() + GetMethod() + "?" + GetParameters();
Run Code Online (Sandbox Code Playgroud)

c++ winapi mfc cstring

2
推荐指数
1
解决办法
3万
查看次数

有哪些技术可以测试字符串是否都是空格?

char *p = "   woohoo";

int condition = /* some calculation applied to p            */ 
                /* to look for all 0x20/blanks/spaces only  */ 

if (condition)
{
}
else
{
    printf("not ");
}

printf("all spaces\n");
Run Code Online (Sandbox Code Playgroud)

c cstring

2
推荐指数
1
解决办法
204
查看次数

遍历嵌套的字符串向量

我的代码中存在一个嵌套字符串向量的问题.它不打印字符串.

void foo(vector<vector<char const *> > const & vcp){
   vector<vector<char const *> >::const_iterator i(vcp.begin());
   vector<vector<char const *> >::const_iterator e(vcp.end());

   for(; i != e; ++i){
      vector<char const *>::const_iterator ci(i->begin());
      vector<char const *>::const_iterator ce(i->end());
      for(; ci != ce; ++ci) 
         cout<<*ci<<endl; //Not printing
   } 
}

int main(){
  std::vector<vector<char const *> > vvcp(3);
  std::vector<char const *> vcp(3);
  vcp.push_back(string("abcd").c_str());
  vcp.push_back(string("efgh").c_str());
  vcp.push_back(string("ijkl").c_str());

  vvcp.push_back(vcp);
  vvcp.push_back(vcp);
  foo(vvcp);
  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

c++ string vector cstring

2
推荐指数
1
解决办法
410
查看次数

C strcat垃圾字符

我在C中有一个函数,我试图从两个不同的位置(未知的大小,可能是安静的大)获取字符串,并将它们组合成一个字符串并返回它们.如果我只打印两个字符串然后我得到正确的结果,但是当我尝试使用strcat组合字符串时,我最终得到5个垃圾字符,然后是组合字符串的结果.

有人对我做错了什么有一些建议吗?以下是一些示例代码,用于演示我正在做的事情:

static int get_information(char** results)
{
    size_t s1_length;
    size_t s2_length;

    /* DEBUGGING - Prints the correct string */
    printf(get_string_1());
    printf(get_string_2());
    printf("\n");

    /* Allocate memory for new string */
    s1_length = strlen(get_string_1());
    s2_length = strlen(get_string_2());
    *results = malloc(sizeof(char) * (dir_length + file_length));

    if(results == NULL)
        return -1;

    /* Combine the strings */
    strcat(*results, get_string_1());
    strcat(*results, get_string_2());

    /* DEBUGGING - prints 5 garbage characters then the correct string */   
    printf(*results);
    printf("\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c cstring strcat

2
推荐指数
2
解决办法
6353
查看次数

使用std :: strings和c-style字符串时如何使用模板?

当我尝试这样做时,我只是在搞乱模板:

template<typename T> void print_error(T msg)
{
#ifdef PLATFORM_WIN32
    ::MessageBox(0, reinterpret_cast< LPCSTR >(msg), "Error", MB_ICONERROR|MB_OK);
#else
    cout << msg << endl;
#endif /* PLATFORM_WIN32 */
}
Run Code Online (Sandbox Code Playgroud)

当然,如果你通过std::stringas,这显然是行不通的T.因为字符串不能转换为a char*,但是这个函数是否可以以允许我传递c样式char*数组和c ++ std::string作为参数的方式进行编码,并将它们转换为LPCSTR

c++ string templates cstring lpcstr

2
推荐指数
1
解决办法
396
查看次数

使用后需要删除CString来释放内存吗?

如果我使用这样的CString:

void myFunc(char *str)
{
  CString s(str);
  // Manipulate other data with CString
  // ...
  // Finished

  // Should I somehow delete 's' here to avoid a memory leak?
}
Run Code Online (Sandbox Code Playgroud)

一旦功能超出范围,字符串是否被删除?

另外,我知道new关键字分配内存,如果构造一个没有new关键字的对象,是否仍然分配了内存?我的直觉告诉我是的,但我想验证.

例如

CString *asdf = new CString("ASDF");
// same as?
CString asdf("ASDF"); 
Run Code Online (Sandbox Code Playgroud)

c++ mfc cstring visual-c++

2
推荐指数
1
解决办法
1万
查看次数

C++ 字符串到固定大小的字符数组可能吗?

嗨,我有以下代码:

char msg[10000];
string mystr = "hello";
Run Code Online (Sandbox Code Playgroud)

我想将 mystr 放入 msg 中。有没有办法做到这一点?我尝试了各种方法,但不断得到:

incompatible types in assignment of 'const char*' to char [10000]'
Run Code Online (Sandbox Code Playgroud)

我试过:

msg = mystr.c_str();
Run Code Online (Sandbox Code Playgroud)

msg = (char[10000])mystr;
Run Code Online (Sandbox Code Playgroud)

无济于事。

c++ string std cstring

2
推荐指数
1
解决办法
5430
查看次数

如何在C++中摆脱CString中的字符串

我所说的 CString 类型是微软的.

我尝试了两种方法;

方法 1) 使用在 msdn 网站上找到的 Remove() 和 Replace() 函数,Visual c++ 说 Error:argument of type "const char*" is incompatible with argument of type "wchar_t"

具体我的代码是:

#include <cstring>

CString sTmp= sBody.Mid(nOffset);
CString sFooter= L"https://" + sTmp.Mid(0, nEnd );
sFooter.Remove("amp;");
Run Code Online (Sandbox Code Playgroud)

而且我不想一次删除一个字符,因为它会去掉其他的 'a'。

出于某种原因,当我将 sFooter 转换为 std::string 时,随机的“amp;”出现在不应该存在的字符串中......

我正在将 sFooter 转换为 std::string ,如下所示:

CT2CA p (sFooter);
string str (p);
Run Code Online (Sandbox Code Playgroud)

方法2)将CString转换为std::string,然后使用erase()和remove()去掉“amp;”

#include <string>
#include <algorithm>

sFooter2.erase(std::remove(sFooter2.begin(), sFooter2.end(), "amp;"), sFooter2.end());
Run Code Online (Sandbox Code Playgroud)

但 Visual Studio 的输出显示了这一点:http : //pastebin.com/s6tXQ48N

c++ string arguments cstring visual-c++

2
推荐指数
1
解决办法
6055
查看次数

错误:转换为非标量类型

我正在为一个赋值创建一组派生类.我被指示使用char数组(c-strings).当我编译时,我不断收到错误:

Homework11.cpp: In function âint main()â:
Homework11.cpp:72: error: conversion from âchar [10]â to non-scalar type âBusinessâ requested
Homework11.cpp:73: error: conversion from âchar [10]â to non-scalar type âBusinessâ requested
Homework11.cpp:74: error: conversion from âchar [10]â to non-scalar type âAccountâ requested
Homework11.cpp:75: error: conversion from âchar [10]â to non-scalar type âAccountâ requested
Run Code Online (Sandbox Code Playgroud)

我相当肯定我的问题源于我尝试将实例变量Name设置为发送的参数.这是我的代码,其中包含注释,我相信问题可能存在.

#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

class Person{
public:
        Person() {}
        Person(char theName[]) {strcpy(name,theName);}
        void getName(char theName[]) // I think the problem may be …
Run Code Online (Sandbox Code Playgroud)

c++ cstring

2
推荐指数
1
解决办法
5万
查看次数

从ATL :: CString转换为C++中的字符串

可能重复:
如何将CString和:: std :: string :: std :: wstring互相转换?

好吧,我知道我的问题可能很简单,但我还没有找到如何将ATL :: Cstring转换为基本字符串.我已尝试过页面http://msdn.microsoft.com/en-us/library/ms235631%28v=vs.80%29.aspx的"从CString示例转换",但它不起作用.任何适当的迹象?

c++ string cstring

1
推荐指数
1
解决办法
4805
查看次数

标签 统计

cstring ×10

c++ ×8

string ×5

c ×2

mfc ×2

visual-c++ ×2

arguments ×1

lpcstr ×1

std ×1

strcat ×1

templates ×1

vector ×1

winapi ×1