In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-10-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how C++ cuts String objects". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how C++ cuts String objects.
Kernel function
The function implemented by the code is to call the find function and the substr function in the String library.
Find function size_type find (const basic_string & str,size_type pos=0)
The find function does the simplest search, looking for the string (str) specified by the parameter, and if it finds a value that returns a String::size_type, it represents the subscript where the match occurs. If it is not found, a static member named string::npos is returned. The C++ standard library defines npos as a const string:: size_type type and initializes it with a value of-1. Because npos is a unsigned type, this initial value means that npos is equal to the maximum possible size of any string.
Parameter str,pos indicates that the search for str starts from the pos location
In addition to find operations, string supports the following operations
Str.find (args) finds the location of the first occurrence of args in str
Str.rfind (args) finds the location where args last appeared in str
Str.find_first_of (args) finds the location where the first character in args appears for the first time in str
Str.find_last_of (args) finds the location of the last occurrence of the first character in args in str
Str.find_first_not_of (args) finds the first character in args in str that is not in atgs
Str.find_last_not_of (args) finds the last character in args in str that is not in atgs
Substr function string substr (size_type pos=0,size_type count=npos)
The main function of substr is to copy strings, which requires you to start at the specified location pos and have a specified length count. If no length or pos+count > source string length is specified, the substring will continue to the end of the source string
Numerical conversion
Since we need to convert string types to int types in this sample, we will briefly introduce the following common numerical conversions for string
Convert int to string
Int iTunes 42 * * string s=to_string (I)
Convert string to int
String str='42';stoi (str); input sample nums = [3meme 2jue 4], target = 6 output style
3 2 4
six
Code implementation
# include#include#includeusing namespace std;void spiltStr (string str,const string & split,vector&strlist) {strlist.clear (); if (str== "") return; string strs=str+split; size_t pos=strs.find (split); int steps=split.size (); while (poster strings. Npos) {/ / substr copy string, starting position, number of copied characters string temp=strs.substr (0pencepos) Strlist.push_back (temp); strs=strs.substr (pos+steps,strs.size ()); pos=strs.find (split);}} int main () {vectornum; vectorstrlist; string inputStr; string tempStr; int target; getline (cin,inputStr); if (inputStr== ") {return 0;} / / split the input string spiltStr (inputStr," [", strlist) SpiltStr (strlist [1], "]", strlist); spiltStr (strlist [0], ",", strlist); / / write the value of vectorspiltStr to vectornums for (auto i:strlist) {num.push_back (stoi (I));} spiltStr (inputStr, "=", strlist); / / spiltStr (strlist.back (), "=", strlist); target=stoi (strlist.back ()) For (auto i:num) {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.