我想反转字符串中的子字符串。例如
string x = "foobar";
所以我想将foo
反转为oof
,所以x将是"oofbar";
这是我尝试过的示例:
string x = "foobar";
size_t pos = x.find("foo");
reverse(x.begin() + k, s.end()); // I got stuck here.
答案 0 :(得分:2)
对于一个简单的情况
string x = "foobar";
您可以使用
std::reverse(x.begin(), x.begin() + 3); // reverse the first three letters.
如果"foo"
嵌入在字符串中,即不在字符串的开头,则必须首先找到其位置。
string x = "whoknowswhereitisfoobar";
auto loc = x.find("foo");
if ( loc != std::string::npos )
{
std::reverse(x.begin() + loc, x.begin() + loc + 3);
}
如果您不想对数字3进行硬编码,则可以使用std::string::size()
:
string hay = "whoknowswhereitisfoobar";
string needle = "foo";
auto loc = hay.find(needle);
if ( loc != std::string::npos )
{
std::reverse(hay.begin() + loc, hay.begin() + loc + needle.size());
}
答案 1 :(得分:1)
鉴于0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli '~\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'start' ]
2 info using npm@5.6.0
3 info using node@v8.6.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle flow-trace-analizer@1.2.2~prestart: flow-trace-analizer@1.2.2
6 info lifecycle flow-trace-analizer@1.2.2~start: flow-trace-analizer@1.2.2
7 verbose lifecycle flow-trace-analizer@1.2.2~start: unsafe-perm in lifecycle true
8 verbose lifecycle flow-trace-analizer@1.2.2~start: PATH: ~\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;~\flow-trace-analizer\node_modules\.bin;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;~\apache-maven-3.3.9\bin;C:\Program Files\nodejs\;C:\Program Files\Git\bin;~\AppData\Roaming\npm;C:\Program Files (x86)\WebEx\PTools020000000;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Program Files\R\R-3.5.0\bin\R.exe;~\Gradle\gradle-4.7\bin;~\Groovy\groovy-2.4.12\bin;C:\Program Files\Heroku\bin;~\AppData\Roaming\npm
9 verbose lifecycle flow-trace-analizer@1.2.2~start: CWD: ~\flow-trace-analizer
10 silly lifecycle flow-trace-analizer@1.2.2~start: Args: [ '/d /s /c', 'node ./bin/www' ]
11 silly lifecycle flow-trace-analizer@1.2.2~start: Returned: code: 3 signal: null
12 info lifecycle flow-trace-analizer@1.2.2~start: Failed to exec start script
13 verbose stack Error: flow-trace-analizer@1.2.2 start: `node ./bin/www`
13 verbose stack Exit status 3
13 verbose stack at EventEmitter.<anonymous> (
~\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\index.js:285:16)
13 verbose stack at emitTwo (events.js:125:13)
13 verbose stack at EventEmitter.emit (events.js:213:7)
13 verbose stack at ChildProcess.<anonymous> (
~\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack at emitTwo (events.js:125:13)
13 verbose stack at ChildProcess.emit (events.js:213:7)
13 verbose stack at maybeClose (internal/child_process.js:927:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
14 verbose pkgid flow-trace-analizer@1.2.2
15 verbose cwd ~\flow-trace-analizer
16 verbose Windows_NT 6.1.7601
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "~\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
18 verbose node v8.6.0
19 verbose npm v5.6.0
20 error code ELIFECYCLE
21 error errno 3
22 error flow-trace-analizer@1.2.2 start: `node ./bin/www`
22 error Exit status 3
23 error Failed at the flow-trace-analizer@1.2.2 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 3, true ]
和auto x = "foobar"s
,我可能只是出于方便而使用迭代器:
const auto value = "foo"s