473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

question on two lines of code

I was trying to read this code and see what it does and I know it is a fuction to a bigger code and is passed some arguments I just had a couple of questions. the first one is temp part of the language since they didn't declare it and the part of the code in bold im not sure what those lines do. Can anyone help

string::size_type loc;
if(loc==string::npos);



Expand|Select|Wrap|Line Numbers
  1. vector<string> split(const string& temp, char s)
  2. {
  3.   string find;
  4.   vector<string> _find;
  5.   string::size_type loc;
  6.   for(int i=0,j=0;i<temp.size(); i++)
  7.     {
  8.         find.clear();
  9.         loc = temp.find(s,i);
  10.  
  11.       if(loc==string::npos)
  12.             return _find;
  13.  
  14.         while(j!=loc)
  15.           {
  16.             find += temp[j];
  17.             j++;
  18.           }
  19.         i += j;
  20.  
  21.        _find.push_back(find);
  22.     }
  23.    return _find;
  24. }
Sep 25 '07 #1
3 1174
Savage
1,764 Expert 1GB
string::size_type loc;//This variable represent current location in the string
if(loc==string::npos);//You might wont to see this

Savage
Sep 25 '07 #2
gsi
51
Hi,
size_t is defined in <cstddef>. It is typically used to represent size in bytes, so defined as an unsigned integral type. This is preferred over 'int' data type bcos of the bigger values it can represent (Unsigned).

string::npos is a static member constant which is of 'unsigned' integral type. It is assigned a value of ' -1' . hence it wraps around to give you the maximum value that could be represented using an unsigned integral type. It is returned by the find member function of the std::string class to indicate a failiure (Absence of a match you are requesting).

eg.
[code = cpp]
string::size_type loc;
if(loc==string::npos);
[/code]

In this two lines of code, the author is trying to figure out if the return value from the find member function is whether a valid position or a failiure.

Thanks,
Gsi.
Sep 25 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
string::size_type loc;
if(loc==string::npos);
A size_type is a typedef of a size_t which is a typedef of an unsigned int.

So loc is just an unsigned int.

string::npos is peculiar to std::string. It represents a number larger than any possible string. Usually (but not always, it is -1). Many of the string methods use string::npos to indicate not found in searches.
Sep 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: Uwe Mayer | last post by:
Hi, I have the following inter-class relationships: __main__: (in file LMCMain.py) imports module FileIO defines class LMCMain instanciats main = LMCMain(...) FileIO.py:
3
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can...
14
by: laniik | last post by:
Hi. Im just wondering what the syntax to overload a casting operator is. For example, i have struct Point { float x,y,z; }; struct Vector { float a,b,c; };
41
by: GC | last post by:
Within a class, do you use the private keyword for your internal member variables or do you leave it off like you would for variables within a function?
12
by: Mark | last post by:
Apologies for the lengthy posting.... In a previous posting:...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
29
by: Greg | last post by:
Now can someone help me answer this question? Northwind Traders is a chain of department stores located around the country. It is setting up a new sales system cashiers will use to accept...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
60
by: Mark Healey | last post by:
I have a function that runs through a two dimensional array several times. 01 x=map->X; 02 do 02 { 04 y=map->Y; 05 do 05 { 06 stuff(); 07
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.