C++中指针与std::string的异常行为问题咨询
Hey there! I see you’ve already got a working version using string library functions to strip out stuff between two * characters, but it sounds like you’re running into some specific issues with it—let’s break down common snags and how to fix them, since this kind of string handling often trips people up with edge cases.
Basic Implementation (What You Might Already Have)
First, let’s recap a standard approach that handles simple, non-nested asterisk pairs—this is probably close to what you’ve built:
#include <string> #include <iostream> std::string stripBetweenAsterisks(std::string input) { size_t openAsterisk = input.find('*'); while (openAsterisk != std::string::npos) { // Find the next closing asterisk after the open one size_t closeAsterisk = input.find('*', openAsterisk + 1); if (closeAsterisk == std::string::npos) { break; // No matching closing asterisk—stop processing here } // Erase from the open asterisk to the close one (inclusive) input.erase(openAsterisk, closeAsterisk - openAsterisk + 1); // Look for the next open asterisk in the modified string openAsterisk = input.find('*'); } return input; } int main() { std::string testStr = "Hello*this is a test*world*another section*!"; std::cout << stripBetweenAsterisks(testStr) << std::endl; // Output: "Hello world!" return 0; }
Common Issues & Quick Fixes
Chances are your problem falls into one of these categories—let’s tackle each:
1. Unmatched Asterisks at the End
If your string ends with an unclosed * (like abc*def*ghi*), the basic code leaves that trailing asterisk hanging. To fix this, add a quick check after the loop to clean it up:
// Add this right before returning the string size_t trailingAsterisk = input.find('*'); if (trailingAsterisk != std::string::npos) { input.erase(trailingAsterisk); }
2. Nested Asterisk Pairs
If you’re dealing with nested asterisks (like abc*def*ghi*jkl*mno), the basic code only removes the first inner pair (def) and leaves the rest. To handle nested pairs (removing from the first open * to the final matching close *), use a counter to track open/close state:
std::string stripNestedAsterisks(std::string input) { size_t openAsterisk = input.find('*'); while (openAsterisk != std::string::npos) { int asteriskCount = 1; size_t closeAsterisk = openAsterisk + 1; // Traverse until we find the matching close asterisk while (closeAsterisk < input.size() && asteriskCount > 0) { if (input[closeAsterisk] == '*') { asteriskCount--; } else if (input[closeAsterisk] == '*') { asteriskCount++; // Count nested open asterisks } closeAsterisk++; } if (asteriskCount == 0) { closeAsterisk--; // Adjust back to the actual closing asterisk input.erase(openAsterisk, closeAsterisk - openAsterisk + 1); } else { // No matching close asterisk—erase the remaining open one and exit input.erase(openAsterisk); break; } openAsterisk = input.find('*'); } return input; }
Testing this with abc*def*ghi*jkl*mno will give you abcmno, which handles the nested pairs correctly.
3. Preserving the Asterisks Themselves
If you want to keep the asterisks but only remove the content between them (e.g., abc*def*ghi → abc**ghi), adjust the erase logic to skip the asterisks:
// Replace the erase line in the basic code with this input.erase(openAsterisk + 1, closeAsterisk - openAsterisk - 1); // Jump past the remaining asterisk for the next iteration openAsterisk = input.find('*', openAsterisk + 2);
Wrap-Up
These tweaks should cover most common issues you might be facing. If you’ve got a specific error message, weird edge case, or a snippet of your current code that’s misbehaving, feel free to share it and we can refine this further!
内容的提问来源于stack exchange,提问作者King Powa

