我需要动态创建一个 char[] 。我想使用 std::string 而不是直接处理 char[],因为我不需要使用 std::string 来处理内存管理。这是我的示例代码:
int main()
{
std::vector<char*> char_vec;
for(size_t i = 0; i < 10; i++)
{
std::string s;
s = "test" + std::to_string(i);
char_vec.push_back(const_cast<char*>(s.c_str()));
}
for(auto& ele : char_vec)
{
std::cout << ele << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
以我的理解,循环内部创建的字符串在每次迭代中都应该是全新的,但实际上,所有字符串都使用相同的地址来存储内容。为什么会出现这种情况?我应该如何修改我的代码以实现我的目标?
感谢您的所有帮助!
2023年1月10日更新:
我根据答案尝试了以下代码:
int main()
{
std::vector<char*> char_vec;
std::vector<std::string> str_vec;
for(size_t i = 0; i < 10; i++)
{
// str_vec.emplace_back("test" + std::to_string(i));
str_vec.push_back("test" + std::to_string(i));
char_vec.push_back(const_cast<char*>(str_vec.back().c_str()));
}
for(auto& ele : char_vec)
{
std::cout …Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手,最近通过 u-demy 讲座学习了指针和参考资料。讲座结束后,我正在解决使用单独的函数反转字符串的问题。我想出了一个代码,但由于某种原因,我可以使用 [] 表示法访问单个字符(就像reverse[0]等于!)但是当我打印整个字符串时,我得到一个空字符串?是什么导致了这个问题?
提前感谢您的宝贵时间!
这里我粘贴了我的代码:
#include <iostream>
// using namespace std;
std::string reverse_string(const std::string &str) {
std::string reversed;
size_t size {str.length()};
for(int i{}; i < size/2; i++) {
reversed[i] = str[(size-1)-i];
reversed[(size-1)-i] = str[i];
}
return reversed;
}
int main() {
// Write C++ code here
std::string input = "Hello, World!";
std::string reversed = reverse_string(input);
std::cout << reversed << std::endl; //I get nothing, BUT
std::cout << reversed[0] << std::endl //here I do get a …Run Code Online (Sandbox Code Playgroud) 我有以下字符串:
index 0 1 2 3 4 5 6 7
std::string myString with the content of "\xff\xff\xff\x00\xff\x0d\x0a\xf5"
Run Code Online (Sandbox Code Playgroud)
当我引用myString [3]时,我得到了预期的'\ x00'值.
但是当我指的是myString [5]时,我得到两个值"\ x0d\x0a"而不仅仅是'\ x0d'.
更有趣的是myString [6]值,即'\ xf5'.这次它就像\ x0d不存在而且引用了正确的位置.
我的问题是:std:string对象中的\ x0d字符有什么特别之处?为什么索引时会跳过它?就像这样计算:
index 0 1 2 3 4 5 5 6
std::string myString = "\xff\xff\xff\x00\xff\x0d\x0a\xf5"
Run Code Online (Sandbox Code Playgroud)
作为注释,'\ x0d'字符是第13个ASCII字符"回车",'\ x0a'是换行符.
更新:可以是std :: string将"\ x0d\x0a"视为单个字符,因此只占用字符串中的一个位置吗?这个'\ x0d'是关于std :: string的"神秘"字符吗?
我在过去的2个小时里一直在调试这个错误,并且知道如果我在睡觉前没有求助,我将无法入睡.我正在为我的游戏编写一个模型加载器,而且就目前而言,我正在使用一种非常脆弱的方法来分割字符串.但是,它在几乎相同的线上工作,然后随机不工作.我正在使用string.substr(),我相信错误意味着它试图从字符串中不存在的位置开始.调用堆栈说它发生在这一行:
v1 = v1.substr(s.find(",")+1);
Run Code Online (Sandbox Code Playgroud)
并且通过使用打印消息的断点,它说
顶点1使用"1,1",整个字符串是"173,1,1 175,1,1 174,1,1"
其中Vertex 1是v1的值,string是s的值.
这是整个功能:
FaceData data;
s = s.substr(5); //remove "FACE "
string v1, v2, v3;
//vertex 1
v1 = s.substr(0, s.find(" "));
data.vertexIndexes[0] = atoi(v1.substr(0, s.find(",")).c_str());
v1 = v1.substr(s.find(",")+1);
data.textureIndexes[0] = atoi(v1.substr(0, s.find(",")).c_str());
v1 = v1.substr(s.find(",")+1);
data.normalIndexes[0] = atoi(v1.c_str());
//vertex 2
s = s.substr(s.find(" ")+1);
v2 = s.substr(0, s.find(" "));
data.vertexIndexes[1] = atoi(v2.substr(0, s.find(",")).c_str());
v2 = v2.substr(s.find(",")+1);
data.textureIndexes[1] = atoi(v2.substr(0, s.find(",")).c_str());
v2 = v2.substr(s.find(",")+1);
data.normalIndexes[1] = atoi(v2.c_str());
//vertex 3
s = s.substr(s.find(" …Run Code Online (Sandbox Code Playgroud) 我在使用时遇到了一些对我没有多大意义的东西std::string::find()我希望这里的某个人能够发现我的错误:
std::string testString = "PFAIL";
std::string response = "PFAIL\n";
if( response.find(testString) != std::string::npos )
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
此代码永远不会//do something因为某些原因而发表评论.有任何想法吗?
编辑:我的意思是它永远不会遇到//do something代码块,它应该如果我用以下方式表达它会这样做:
if( response.find( testString.c_str() ) != std::string::npos )
{
// do something
}
Run Code Online (Sandbox Code Playgroud) BC++ 方法在输入时返回字母表,A如此直到Z然后如果输入是Z该方法应该返回AA如果输入是AA该方法应该返回AB,依此类推直到ZZ。请找到我正在尝试的示例程序。
void getString(string s){
for (char ch = 'A'; ch<= 'Z';)
{
cin >> ch;
ch++;
cout<< ch;
if (ch = 'Z')
{
cout << "in loop";
for (char k = 'A'; k<= 'Z';){
for (char j = 'A'; j<= 'Z';j++){
char res = k + j;
cout << res;
}
k++;
}
}
}
}
int main() {
getString("");
return 0;
}
Run Code Online (Sandbox Code Playgroud) string A = "LOLWUT";
cout << A.substr(0, A.length() - 1) << endl;
cout << A.substr(1, A.length() - 1) << endl;
Run Code Online (Sandbox Code Playgroud)
此代码打印:
LOLWU
OLWUT
Run Code Online (Sandbox Code Playgroud)
根据我的理解,应该是:
LOLWU
OLWU
Run Code Online (Sandbox Code Playgroud)
为什么?我缺少什么?这是我今天工作时遇到的一个简单的事情,我可以解决它,但我想理解它。
我的 g++ 版本:
g++(MinGW-W64 x86_64-ucrt-posix-seh,由布莱希特·桑德斯构建)13.1.0
我找不到一种方法让我的终端在空输入时退出程序。我有:
int main(int argc, char const* argv[]) {
// Write your code here
// Define variables
set<string> words; // The set
bool more = true; // Flag indicating there are more lines to read
// If there is no command line argument, use stdin to get the lines
if (argc == 1){
// Take lines in until a blank line is entered
while (more) {
// Get next line from stdin
string input;
cin >> input;
// Quit if we …Run Code Online (Sandbox Code Playgroud) 奇怪的行为。我正在用 C++ 开发 android 本机应用程序,但遇到了错误。某些函数由于某种原因没有被调用。经过一些恢复和比较..
这给程序带来了麻烦..
const std::string STR_PATH_ASSET("assets/");
const std::string STR_PATH_SD("/sdcard/unlock_data/assets/");
const std::string STR_SUFFIX_PNG(".png");
const std::string STR_SUFFIX_KTX(".ktx");
Run Code Online (Sandbox Code Playgroud)
这使程序工作..
std::string const STR_PATH_ASSET("assets/");
std::string const STR_PATH_SD("/sdcard/unlock_data/assets/");
std::string const STR_SUFFIX_PNG(".png");
std::string const STR_SUFFIX_KTX(".ktx");
Run Code Online (Sandbox Code Playgroud)
无论如何它都有效,但我不知道为什么这种差异会导致如此奇怪的行为。有什么猜想吗??
添加了完整的源代码。
这不仅是“std::string const”与“const std::string”的问题,而是声明本身。对不起。
这是我的源代码。当我取消注释那些 std::string 的东西时,它无法正常工作。我在我的 android 上画了一些东西,但是当使用 std::string 东西时,一些网格(顶点)的初始位置不同。从逻辑上讲,它不能影响这个常量是否存在。我在 windows cygwin 上使用 ndk 编译器版本 4.6,ndk 版本 14。这个 const std::string 声明会影响另一个堆栈的内存吗?IE。transh值什么的?
typedef enum _ImageCompressType{
//REF http://stackoverflow.com/questions/9148795/android-opengl-texture-compression
COMPRESS_UNAVAILABLE = -1,
COMPRESS_ETC1 = 1,
COMPRESS_PVRTC,
COMPRESS_ATITC,
COMPRESS_S3TC
}ImageCompressType;
typedef enum _FileDataFrom{
FROM_ASSET, FROM_SD
}FileDataFrom;
//std::string const STR_PATH_ASSET("assets/");
//std::string const …Run Code Online (Sandbox Code Playgroud) 可以std::string在C++ 11中复制构造函数吗?
(Stackoverflow说我的问题不符合其质量标准,似乎它只是想要更多的散文,所以这里有一些虚拟文本.)
我有一小段C++代码:
#include <array>
#include <string>
#include <iostream>
int main()
{
std::string name = "mario";
std::cerr << "Hello world! " + name + "\n";
std::array<float, 4> arr = {12, 12.3, 13, 14};
std::cerr << "first item is: " + std::to_string(arr.front()) << std::endl;
std::cerr << "last item is: " + std::to_string(arr[-1]) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译并输出以下内容:
work ? c++ -std=c++11 -o hello_world hello.cpp
work ? ./hello_world
Hello world! mario
first item is: 12.000000
last item is: 0.000000
Run Code Online (Sandbox Code Playgroud)
但是,如果我注释掉前两行,如:
#include <array>
#include …Run Code Online (Sandbox Code Playgroud) 为什么C++ 在编写代码时没有隐式转换为bool定义for std::string和STL容器
if (!x.empty()) { ... }
Run Code Online (Sandbox Code Playgroud)
而不是更短
if (x) { ... }
Run Code Online (Sandbox Code Playgroud)
当x类型是std::string或例如std::vector?
我也对std::string(在C++ 03中)没有隐式转换为const char*STL示例的事实感到困惑
std::string s("filename");
std::ofstream(s.c_str();
Run Code Online (Sandbox Code Playgroud) 为什么std::to_string允许您将 a 转换double为std::stringwhen std::is_convertible_v<double,std::string>is false?例如:
#include<iostream>
#include<type_traits>
#include<string>
int main()
{
std::cout << "If double is "
<< (std::is_convertible_v<double,std::string> ? "" : "not ")
<< "convertible to std::string then why is it possible to do this? "
<< std::to_string(3.141592) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)