Is std::string::np os portably able to be incremented?
For example, I want to insert some text into a string. If a certain
character is found, I want to insert this text immediately after this
character; otherwise I insert at the beginning of the string. On my
implementation string::npos has the value of (string::size_t ype)-1, so
incrementing it will make it 0, but can I rely on it?
Also, say that the character occurs at the end of the string. Is it
valid to specify the insert position as one greater than this value?
Example usage of what I want to do is shown in fix_path(). Everything
behaves as I would expect on my implementation (VS 2005).
#include <iostream>
#include <string>
void fix_path(std::s tring& s)
{
using std::string;
string to_insert = "goes/";
string::size_ty pe pos = s.rfind('/');
s.insert(pos + 1, to_insert);
}
void test(const std::string& s)
{
using std::cout;
using std::string;
string t(s);
cout << t << '\n';
fix_path(t);
cout << t << "\n\n";
}
int main()
{
using std::cout;
using std::string;
test("my/stuff/here");
test("here");
test("another/");
test("/beginning");
}
/*
Output:
my/stuff/here
my/stuff/goes/here
here
goes/here
another/
another/goes/
/beginning
/goes/beginning
*/
--
Marcus Kwok
Replace 'invalid' with 'net' to reply 8 2541
Marcus Kwok wrote:
Is std::string::np os portably able to be incremented?
For example, I want to insert some text into a string. If a certain
character is found, I want to insert this text immediately after this
character; otherwise I insert at the beginning of the string. On my
implementation string::npos has the value of (string::size_t ype)-1, so
incrementing it will make it 0, but can I rely on it?
Also, say that the character occurs at the end of the string. Is it
valid to specify the insert position as one greater than this value?
Example usage of what I want to do is shown in fix_path(). Everything
behaves as I would expect on my implementation (VS 2005).
#include <iostream>
#include <string>
void fix_path(std::s tring& s)
{
using std::string;
string to_insert = "goes/";
string::size_ty pe pos = s.rfind('/');
s.insert(pos + 1, to_insert);
}
void test(const std::string& s)
{
using std::cout;
using std::string;
string t(s);
cout << t << '\n';
fix_path(t);
cout << t << "\n\n";
}
int main()
{
using std::cout;
using std::string;
test("my/stuff/here");
test("here");
test("another/");
test("/beginning");
}
/*
Output:
my/stuff/here
my/stuff/goes/here
here
goes/here
another/
another/goes/
/beginning
/goes/beginning
*/
Would boost::regex_re place be a better choice in this case?
Fei
Marcus Kwok wrote:
Is std::string::np os portably able to be incremented?
By definition, npos is 'size_type' and has the value -1. See 21.3/6.
For example, I want to insert some text into a string. If a certain
character is found, I want to insert this text immediately after this
character; otherwise I insert at the beginning of the string. On my
implementation string::npos has the value of (string::size_t ype)-1
It is required to be that on all implementations .
>, so
incrementing it will make it 0, but can I rely on it?
I believe so.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Fri, 29 Jun 2007 15:14:34 +0000, Marcus Kwok wrote:
Is std::string::np os portably able to be incremented?
For example, I want to insert some text into a string. If a certain
character is found, I want to insert this text immediately after this
character; otherwise I insert at the beginning of the string. On my
implementation string::npos has the value of (string::size_t ype)-1, so
incrementing it will make it 0, but can I rely on it?
Most likely yes, since the standard defines 'npos' as:
static const size_type npos = -1;
And 'size_type' is:
typedef typename Allocator::size _type size_type;
--
Obnoxious User
Victor Bazarov <v.********@com acast.netwrote:
Marcus Kwok wrote:
>>, so incrementing it will make it 0, but can I rely on it?
I believe so.
Thanks. Do know the answer to my other question (whether or not it is
well-defined to specify s.size() as the insert position for string)?
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Marcus Kwok wrote:
Victor Bazarov <v.********@com acast.netwrote:
>Marcus Kwok wrote:
>>, so incrementin g it will make it 0, but can I rely on it?
I believe so.
Thanks. Do know the answer to my other question (whether or not it is
well-defined to specify s.size() as the insert position for string)?
It's well defined.
john
John Harrison <jo************ *@hotmail.comwr ote:
Marcus Kwok wrote:
>Thanks. Do know the answer to my other question (whether or not it is well-defined to specify s.size() as the insert position for string)?
It's well defined.
Great, thanks.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
On Jun 29, 5:14 pm, ricec...@gehenn om.invalid (Marcus Kwok) wrote:
Is std::string::np os portably able to be incremented?
The obvious answer is that it's a constant, and you can't
increment a constant. But from the rest of your post, I gather
that what you really want to know is whether npos + 1 is
guaranteed to be zero.
Technically, the answer is no. npos is guaranteed to have the
value (size_t)(-1), and size_t is guaranteed to be an unsigned
type, so the value would be guaranteed to be 0, unless integral
promotion occurs. However:
-- I've never heard of an implementation where size_t was
smaller than an unsigned int, so integral promotion won't
occur, and
-- even if integral promotion occurs, if you immediately
reconvert the results back to a size_t, you're guaranteed to
end up with 0.
So in practice, I think you can count on it.
--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze <ja*********@gm ail.comwrote:
On Jun 29, 5:14 pm, ricec...@gehenn om.invalid (Marcus Kwok) wrote:
-- even if integral promotion occurs, if you immediately
reconvert the results back to a size_t, you're guaranteed to
end up with 0.
So in practice, I think you can count on it.
Thanks for the confirmation. I am just using it as the parameter for
string::insert( ), which is of size_type, so I should be fine.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christopher Benson-Manica |
last post by:
Let's say I have a std::string, and I want to replace all the ','
characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the
following the best way to do it?
int idx;
while( (idx=str.find_first_of(',')) >= 0 ) {
str.replace( idx, 1, "" );
str.insert( idx, " or " );
}
|
by: Derek |
last post by:
A common technique for trimming leading and trailing spaces
from std::string is the following:
string s(" blah blah blah ");
const char* ws= " \t\r";
string::size_type not_white;
// trim leading whitespace
not_white = s.find_first_not_of(ws);
|
by: JKop |
last post by:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.
I've been trying Google for the last 20 minutes but can't
get anything decent.
Thanks.
|
by: Marcus Kwok |
last post by:
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the
"all characters" marker. I tried to use it this way, but my program
crashes:
#include <iostream>
#include <string>
int main()
{
std::string s = "hi/hello/now";
|
by: FBergemann |
last post by:
if i compile following sample:
#include <iostream>
#include <string>
int main(int argc, char **argv)
{
std::string test = "hallo9811111z";
std::string::size_type ret;
| |
by: Christopher Pisz |
last post by:
Is std::string::npos always going to be less than any std::string 's size()?
I am trying to handle a replacement of all occurances of a substr, in which
the replacement also contains the substr. Yick. All I could come up with is:
#include <string>
int main()
{
std::string text;
|
by: Hendrik Schober |
last post by:
Hi,
this
#include <string>
class test {
typedef std::string::size_type size_type;
static const size_type x = std::string::npos;
};
doesn't compile using either VC9 ("expected constant expression")
or Comeau Online ("constant value is not known"). If I replace
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |