任何有关这方面的想法将不胜感激:
std::string s1 = "hello";
std::string s2 = std::string(s1);
Run Code Online (Sandbox Code Playgroud)
我现在期望这两个字符串是独立的,即我可以将",world"附加到s2,而s1仍然会读取"hello".这是我在Windows和Linux上发现的但是在HP_UX机器上运行代码似乎s2和s1是相同的字符串,所以修改s2会改变s1.
这听起来绝对疯狂,有人见过类似的东西吗?
考虑
#include <string>
#include <iostream>
int main()
{
/*
hello
5
hel
3
*/
char a[] = "hello";
std::cout << a << std::endl;
std::cout << strlen(a) << std::endl;
a[3] = 0;
std::cout << a << std::endl;
std::cout << strlen(a) << std::endl;
/*
hello
5
hel o
5
*/
std::string b = "hello";
std::cout << b << std::endl;
std::cout << b.length() << std::endl;
b[3] = 0;
std::cout << b << std::endl;
std::cout << b.length() << std::endl;
getchar();
}
Run Code Online (Sandbox Code Playgroud)
我希望它的std::string行为与 …
为什么没有std::basic_string<...>::back()会员功能?
功能显然在那里,我的意思是,人们可以写
myString[myString.size()-1]
*myString.rbegin()
我假设潜力back()和上述两种方法之间有一些错误的等价吗?你可以说我的第二个版本足够简洁,但是,为什么不提供一个简单的实现功能,使字符串更接近容器?有什么警告我不见了?
谢谢.
鉴于这样的代码段:
#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream file("1.txt");
string str((istream_iterator<char>(file)),istream_iterator<char>());
file.close();
cout<<str<<endl;
}
Run Code Online (Sandbox Code Playgroud)
该代码使用istream_iterator从文件构造一个字符串.
请注意,字符串构造函数的第一个参数用一对括号括起来.如果省略括号,则会出错.在VC++ 2008中,会出现链接错误.在G ++中,代码输出错误.
我对括号感到很奇怪.有什么区别,为什么?
以下代码从char数组中正确删除了标点符号:
#include <cctype>
#include <iostream>
int main()
{
char line[] = "ts='TOK_STORE_ID'; one,one, two;four$three two";
for (char* c = line; *c; c++)
{
if (std::ispunct(*c))
{
*c = ' ';
}
}
std::cout << line << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如果line类型是这样的代码将如何看std::string?
这是一个FAQ,但我找不到令人满意的答案.在我的项目中,我们支持std::string并且现在还必须支持宽字符串.所以我们想要转移basic_string,但是,事情就会停止运行,并且需要明确地拼写参数:
#include <string>
template <typename CharT, typename Traits, typename Allocator>
void
foo(const std::basic_string<CharT, Traits, Allocator>&)
{}
template void foo(const std::string&);
// template void
// foo<char, std::char_traits<char>, std::allocator<char>>(const std::string&);
void bar(const std::string& s)
{}
int main()
{
bar("abc");
foo<char, std::char_traits<char>, std::allocator<char>>("def");
foo("def");
}
Run Code Online (Sandbox Code Playgroud)
好吧,由于众所周知的原因,它失败了:
clang++-mp-3.5 -Wall -std=c++11 foo.cc
foo.cc:20:3: error: no matching function for call to 'foo'
foo("def");
^~~
foo.cc:5:1: note: candidate template ignored: could not match
'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>'
against 'char const[4]'
foo(const std::basic_string<CharT, Traits, Allocator>&) …Run Code Online (Sandbox Code Playgroud) 如果用户在shell命令行中输入"shutdown"或"restart",我试图让我的shell终止.一切都很好.我用C(strtok,cstring)和C++编写代码.当我使用valgrind时,它表示仍然可以访问1个块.我知道这不是泄漏,但我很想知道是什么导致它以及如何解决它.花了2天试图找到它不能.请查看我的代码和valgrind报告.
码
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
using namespace std;
const int MAX_COMMAND_LINE_ARGUMENTS = 8;
const int SLEEP_DELAY = 100000;
int GetCommand (string tokens []);
int TokenizeCommandLine (string tokens [], string commandLine);
bool CheckForCommand ();
void ProcessCommand (string tokens [], int tokenCount);
void ShutdownAndRestart (string tokens [], int tokenCount);
static volatile sig_atomic_t cullProcess = 0;
int main()
{
string tokens [MAX_COMMAND_LINE_ARGUMENTS];
int tokenCount;
do
{
tokenCount = GetCommand (tokens); …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
class myclass
{
std::string str;
public:
void setStr(std::string value)
{
str=value;
}
std::string getStr()
{
return str;
}
}
main()
{
myclass ms;
std::cout<<ms.getStr()<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当我编译并运行此代码时,会出现o错误,在Windows中,我总是将str作为“”。
这一直有效吗?
我需要上述行为,因为如果用户未调用set,str将始终为空字符串。
我应该在构造函数中初始化str如下:
class myclass
{
std::string str;
public:
myclass():str(""){}
void setStr(std::string value)
{
str=value;
}
std::string getStr()
{
return str;
}
}
Run Code Online (Sandbox Code Playgroud)
我想确保所有平台上的行为都相同,并且还要确保代码尽可能小巧,整洁。
我试图通过交换两个连续的字母来进行基本的字符串加密。而且它并没有真正按照我的预期工作。
#include <iostream>
#include <string.h>
#include <algorithm>
int main()
{
std::string str = "This is a simple string.";
for (int i = 0; i <= str.length(); i++) {
std::swap(str[i], str[i + 1]);
}
std::cout << str;
std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)
我实际上想交换两个附近的字母,所以它看起来像加密的。当前结果是
his is a simple string.
Run Code Online (Sandbox Code Playgroud) 我尝试了以下程序:
#include <iostream>
#include <string>
int main ()
{
std::string str;
str[0] = 'o';
str[1] = 'k';
std::cout << str.length();
std::cout << "as a whole :";
std::cout << str << std::endl;
std::cout << "character by character :";
std::cout << str[0] << str[1] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我不能使用object变量将字符串整体打印出来,为什么返回长度,0因为显然我已经使用下标运算符添加了字符,因为这将返回char引用,所以我知道这是合法的。
另外,我没有得到任何例外。就是这样。显然,std::string班上的幕后发生了很多事情,我知道我错过了一些东西。有人可以帮我吗?