我的问题几乎与这个问题相同,但那里的解决方案还没有解决我的错误.
在main.h我有:
#include <map>
#include <string>
std::map<std::string, int64_t> receive_times;
Run Code Online (Sandbox Code Playgroud)
并在main.cpp:
std::map<std::string, int64_t>::const_iterator iter;
std::map<std::string, int64_t>::const_iterator eiter = receive_times.end();
for (iter = receive_times.begin(); iter < eiter; ++iter)
printf("%s: %ld\n", iter->first.c_str(), iter->second);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译时,我收到以下错误:
error: invalid operands to binary expression ('std::map<std::string, int64_t>::const_iterator' (aka '_Rb_tree_const_iterator<value_type>') and 'std::map<std::string, int64_t>::const_iterator'
(aka '_Rb_tree_const_iterator<value_type>'))
for (iter = receive_times.begin(); iter < eiter; ++iter)
~~~~ ^ ~~~~~
Run Code Online (Sandbox Code Playgroud)
我在顶部链接的问题中的解决方案是因为缺少了#include <string>,但显然我已将其包括在内.任何提示?
这是我使用反转字符串的代码std::string.但它不起作用..
#include <string>
#include <iostream>
using namespace std;
main()
{
string input;
int i, j;
cout << "Enter a string: ";
getline(cin,input);
string output;
for(i = 0, j = input.length() - 1; i < input.length(); i++, j--)
output[i]=input[j];
cout << "Reversed string = " << output;
cin.get();
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我们替换字符串输出,char output[100];它会工作.所以std::string不允许角色分配?
我有一个std::string,我该如何替换:字符%%?
std::replace( s.begin(), s.end(), ':', '%%' );
上面的代码不起作用:
错误没有实例匹配争论列表
谢谢!
我确定我做错了什么,但对于我的生活,我无法弄清楚是什么!请考虑以下代码:
cerr<<el.getText()<<endl;
cerr<<el.getText().c_str()<<endl;
cerr<<"---"<<endl;
const char *value = el.getText().c_str();
cerr<<"\""<<value<<"\""<<endl;
field.cdata = el.getText().c_str();
cerr<<"\""<<field.cdata<<"\""<<endl;
Run Code Online (Sandbox Code Playgroud)
el是一个XML元素并getText返回一个std :: string.正如预期的那样,el.getText()和el.getText().c_str()打印相同的值.但是,当它分配结果时,value设置为""- 即空字符串c_str().此代码已写入设置field.cdata=value,因此清除它.将其更改为所谓的相同表达式value后,它可以正常工作,最后一行打印出预期值.
因为el在堆栈中,我认为我可能已经破坏了它 - 但即使在value设置之后,基础值el仍然是正确的.
我的下一个想法是,有一些奇怪的编译器特定问题,将事物分配给const指针,所以我写了以下内容:
std::string thing = "test";
std::cout << thing << std::endl;
std::cout << thing.c_str() << std::endl;
const char* value = thing.c_str();
std::cout << value << std::endl;
Run Code Online (Sandbox Code Playgroud)
正如所料,我进行了三次"测试".
所以现在我不知道发生了什么.很明显,我的程序中有一些奇怪的事情在样本中没有发生,但我不知道它是什么,我不知道如何继续寻找.有人可以启发我,或者至少指出我正确的方向吗?
在C++中,我有一个名为的字符串数组变量:
...
/* set the variable */
string fileRows[500];
...
/* fill the array with a file rows */
while ( getline(infile,sIn ) )
{
fileRows[i] = sIn;
i++;
}
Run Code Online (Sandbox Code Playgroud)
和一个具有以下内容的对象:
string Data::fileName(){
return (fileRows);
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个返回数组的函数,之后我想称之为:
Data name(hwnd);
MessageBox(hwnd, name.fileName(), "About", MB_OK);
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
不能将'std :: string*{aka std :: basic_string }'转换为'LPCSTR {aka const char }'以将参数'2'转换为'int MessageBoxA(HWND,LPCSTR,LPCSTR,UINT)'
如果我想显示数组的5.元素,如何转换它?
当我打电话时,我目前正在收到分段故障(分段故障:11)newUnitID().
不知道我做错了什么.
这是我的头文件,其函数是:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#ifndef UnitManager
#define UnitManager
using namespace std;
char randomIDChar(){
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
srand(time(0));
for(int z=0; z < 21; z++)
{
return alphanum[rand() % stringLength];
}
return 1;
}
string newUnitID(){
vector<char> v;
for(int i=0; i < 50; i++){
v[i] = randomIDChar();
}
string str(v.begin(),v.end());
return str;
}
#endif
Run Code Online (Sandbox Code Playgroud) 我需要使用snprintfwith 替换C char缓冲区std::string并对它们执行相同的操作.我被禁止使用stringstream或boost库.
有办法吗?
const char *sz="my age is";
std::string s;
s=sz;
s+=100;
printf(" %s \n",s.c_str());
Run Code Online (Sandbox Code Playgroud)
我把输出作为
my age is d
Run Code Online (Sandbox Code Playgroud)
所需输出的位置是:
my age is 100
Run Code Online (Sandbox Code Playgroud) 我之前对这个问题有不同的答案,所以决定再问这里.
假设我有一个功能node* foo(),如果有一些失败,我做return NULL.这段代码真的返回NULL指针吗?或者这NULL是一个local temporary object?编译时我没有警告.我还可以写下这样的东西:
node* ptr = foo();
if (ptr)
printf("Not NULL");
else
printf("NULL");
Run Code Online (Sandbox Code Playgroud)
它似乎有效.(但如果这个功能是,例如const string& foo()它没有)
string words[5];
for (int i = 0; i < 5; ++i) {
words[i] = "word" + i;
}
for (int i = 0; i < 5; ++i) {
cout<<words[i]<<endl;
}
Run Code Online (Sandbox Code Playgroud)
我期望结果如下:
word1
.
.
word5
Run Code Online (Sandbox Code Playgroud)
在控制台中打印如下:
word
ord
rd
d
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我这个的原因.我相信在java中它将按预期打印.
我遇到了一些C++ 11 std::string长度的可移植性问题.在Windows上它是long long unsigned int在Linux和Mac上long unsigned int.我的理解是使用auto是一种解决这类问题的标准方法,但是我很难找到一种通过类接口公开这些属性的可移植方法.
以下类在Linux GCC 7.3.0(以及MacOS)上编译和运行没有问题:
g++ -g -O2 -std=c++11 -Werror=conversion stringwrap.cc
./a.out
3
Run Code Online (Sandbox Code Playgroud)
但是在Windows(g ++ 8.1.0 MinGW-W64 x86_64-posix-seh-rev0)上,我得到以下编译错误:
C:\temp\v0.11>g++ -g -O2 -std=c++11 -Werror=conversion stringwrap.cc
In file included from stringwrap.cc:1:
stringwrap.h: In function 'long unsigned int determineFirstPosition(std::__cxx11::string)':
stringwrap.h:35:20: error: conversion from 'std::__cxx11::basic_string<char>::size_type' {aka 'long long unsigned int'}
to 'long unsigned int' may change value [-Werror=conversion]
return s.length();
~~~~~~~~^~
stringwrap.cc: In member function 'long unsigned int Stringwrap::length() const':
stringwrap.cc:9:23: …Run Code Online (Sandbox Code Playgroud)