473,799 Members | 3,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange error with iterators

Hi, I've wrote this function which should add a comma for every 3
digits in a number (so that it looks something like 5,000).

This is my function:
std::string formatNumber(in t number)
{
// Convert the int to a string.
std::string numString = boost::lexical_ cast<std::strin g>(number);

std::string::co nst_reverse_ite rator i = numString.rbegi n(), end =
numString.rend( );
for (int j = 0; i != end; ++i, ++j)
{
// Add a comma every three places.
if (j < 3)
{
// ERROR HAPPENS ON THIS LINE BELOW.
i = numString.inser t(i + 1, ',');
end = numString.rend( );

j = 0;
}
}

return numString;
}

I get an error saying it cannot convert from reverse_iterato r to
unsigned int. It looks to me as if it's trying to call the wrong
overloaded function, as I did a search on the method and I can indeed
pass an iterator and a char.

Can anybody tell me why this is failing?

Thanks in advance.

Aug 23 '07 #1
6 1480
In message <11************ **********@q3g2 000prf.googlegr oups.com>,
Michael Coley <m1****@googlem ail.comwrites
>Hi, I've wrote this function which should add a comma for every 3
digits in a number (so that it looks something like 5,000).

This is my function:
std::string formatNumber(in t number)
{
// Convert the int to a string.
std::string numString = boost::lexical_ cast<std::strin g>(number);

std::string::co nst_reverse_ite rator i = numString.rbegi n(), end =
numString.rend ();
for (int j = 0; i != end; ++i, ++j)
{
// Add a comma every three places.
if (j < 3)
{
// ERROR HAPPENS ON THIS LINE BELOW.
i = numString.inser t(i + 1, ',');
end = numString.rend( );

j = 0;
}
}

return numString;
}

I get an error saying it cannot convert from reverse_iterato r to
unsigned int. It looks to me as if it's trying to call the wrong
overloaded function, as I did a search on the method and I can indeed
pass an iterator and a char.
i+1 isn't an iterator, it's a const_reverse_i terator

--
Richard Herring
Aug 23 '07 #2
Michael Coley wrote:
Hi, I've wrote this function which should add a comma for every 3
digits in a number (so that it looks something like 5,000).

This is my function:
std::string formatNumber(in t number)
{
// Convert the int to a string.
std::string numString = boost::lexical_ cast<std::strin g>(number);

std::string::co nst_reverse_ite rator i = numString.rbegi n(), end =
numString.rend( );
for (int j = 0; i != end; ++i, ++j)
{
// Add a comma every three places.
if (j < 3)
{
// ERROR HAPPENS ON THIS LINE BELOW.
i = numString.inser t(i + 1, ',');
Are you sure you can do +1 to the 'i'? Are you sure you can use the
*const* iterator for that? Just checking. Since your 'numString'
is not a constant object, why do you think you need a _const_ iterator
to work with it?
end = numString.rend( );

j = 0;
}
}

return numString;
}

I get an error saying it cannot convert from reverse_iterato r to
unsigned int. It looks to me as if it's trying to call the wrong
overloaded function, as I did a search on the method and I can indeed
pass an iterator and a char.

Can anybody tell me why this is failing?
Well, try to rewrite this using a non-const reverse iterator, or even
better, not using iterators at all. You know, you can just use
indices...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 23 '07 #3
I thought it would be something stupid.

Thanks to everyone who helped.

Aug 23 '07 #4
In message <fa**********@n ews.datemas.de> , Victor Bazarov
<v.********@com Acast.netwrites
>Michael Coley wrote:
>Hi, I've wrote this function which should add a comma for every 3
digits in a number (so that it looks something like 5,000).

This is my function:
std::string formatNumber(in t number)
{
// Convert the int to a string.
std::string numString = boost::lexical_ cast<std::strin g>(number);

std::string::co nst_reverse_ite rator i = numString.rbegi n(), end =
numString.rend ();
for (int j = 0; i != end; ++i, ++j)
{
// Add a comma every three places.
if (j < 3)
{
// ERROR HAPPENS ON THIS LINE BELOW.
i = numString.inser t(i + 1, ',');

Are you sure you can do +1 to the 'i'? Are you sure you can use the
*const* iterator for that? Just checking.
No problem. The const describes what the iterator refers to, not the
iterator itself, and it models the random-access iterator concept, so
why not?

The real problem is elsewhere...
Since your 'numString'
is not a constant object, why do you think you need a _const_ iterator
to work with it?
> end = numString.rend( );

j = 0;
}
}

return numString;
}

I get an error saying it cannot convert from reverse_iterato r to
unsigned int. It looks to me as if it's trying to call the wrong
overloaded function, as I did a search on the method and I can indeed
pass an iterator and a char.

Can anybody tell me why this is failing?

Well, try to rewrite this using a non-const reverse iterator,
or better, a non-const non-reverse iterator :-/
>or even
better, not using iterators at all. You know, you can just use
indices...
Indeed.

--
Richard Herring
Aug 23 '07 #5
Michael Coley <m1****@googlem ail.comwrote in news:1187868090 .229886.240180
@q3g2000prf.goo glegroups.com:
>
I get an error saying it cannot convert from reverse_iterato r to
unsigned int. It looks to me as if it's trying to call the wrong
overloaded function, as I did a search on the method and I can indeed
pass an iterator and a char.

Can anybody tell me why this is failing?
Because, while you can pass insert() an iterator and a character, you can't pass it a
reverse_iterato r and a char.

joe
Aug 23 '07 #6
In article <11************ **********@q3g2 000prf.googlegr oups.com>,
m1****@googlema il.com says...
Hi, I've wrote this function which should add a comma for every 3
digits in a number (so that it looks something like 5,000).
iostreams can already format numbers like that without your doing all
the work:

First you create a numpunct facet to tell it how you want the formatting
done:

#include <locale>

template <class T>
struct formatter : std::numpunct<T {
protected:
T do_decimal_poin t() const { return T('.'); }
T do_thousands_se p() const { return T(','); }
std::basic_stri ng<Tdo_grouping () const {
return std::basic_stri ng<T>("\3");
}
};

Then you create a locale using that facet, and imbue a stream with the
locale to get it to use it:

std::locale f(std::locale:: classic(), new formatter<char> );
std::cout.imbue (f);

Then when you write numbers to that stream, they're formatted as you've
specified. If you really need to produce a string as the output, you can
imbue a stringstream with a locale as well.

Nicely enough, it's (usually) even easier to do the right thing, and
format the numbers the way the user expects:

std::locale local("");
std::cout.imbue (local);
std::cout << 123456789;

For me, this produces "123,456,78 9" -- but as (for example) a European
would typically configure their machine, it would produce "123.456.78 9"
instead, just as they expect.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 26 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2415
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0), an iterator that I've set in a loop gets changed. Here's an example of the problem (sorry about the lack of indentation, posting this from Google): #include <vector> #include <iostream>
18
2307
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much of a performance hit I'm taking using this technique versus using 'copy' to copy directly into a container with elements in place? Thanks, d
3
2927
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5 kinds of iterators on say a vector. OR is it that any iterator declared on Vector is Random Iterator which has the functionality of all the others.
24
3969
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = s2 = s3 = for value in ???(s1, s2, s3):
1
1924
by: mn04 | last post by:
I am getting the following error message during compilation of my C++ program on XP laptop using .NET libraries: ossxerces_utils.cxx C:\narayan\axcelis\Lib\ossxerces_utils\src\OssDataFileTemplate.cxx(122) : error C2582: 'operator =' function is unavailable in 'std::basic_ofstream<_Elem,_Trait s>' with C:\narayan\axcelis\Lib\ossxerces_utils\src\OssDataFileTemplate.cxx(304) : error C2440: 'return' : cannot convert from...
2
2355
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
5
4206
by: Tin Gherdanarra | last post by:
Dear mpdls, here is a simple example of an IEnumerable that generates integers: It works, but I have only a vague idea of what's going on. I understand that /yield/ wraps the humble integer that comes from counter++
18
2123
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
7
1670
by: utab | last post by:
Dear all, I was experimenting with a template taking two iterators for the range of a vector.(Perhaps, it is sth simple and I am missing it because it is a late hour.) I ran into problems in the compile phase , the code is below: #include <iostream>
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10470
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, 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...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
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,...
0
10023
tracyyun
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...
0
9067
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, 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...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2935
bsmnconsultancy
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...

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.