How to format numbers to significant digits using STL - c++

I'm trying to format numbers to a specific number of significant digits using C/C++ and preferably STL. I've seen examples of doing this in Javascript (toPrecision()) and .Net, but I can't find anything on doing this in C/C++. I want to create a function something like this:
std::string toPrecision(double value, int significantDigits) {
std::string formattedString;
// magic happens here
return formattedString;
}
So that it produces results like this:
toPrecision(123.4567, 2) --> "120"
toPrecision(123.4567, 4) --> "123.4"
toPrecision(123.4567, 5) --> "123.45"
Does anyone know a good way to do this? I'm considering dumping the whole number into a string and then just scanning through it to find the non-zero digits and count them off in some intelligent way, but that seems cumbersome.
I could also download the source code to one of the browsers and just see what their toPrecision function looks like, but I think it would take me all day to work through the unfamiliar code. Hope someone can help!

Stolen from another question:
#include <string>
#include <sstream>
#include <cmath>
#include <iostream>
std::string toPrecision(double num, int n) {
https://stackoverflow.com/questions/202302/rounding-to-an-arbitrary-number-of-significant-digits
if(num == 0) {
return "0";
}
double d = std::ceil(std::log10(num < 0 ? -num : num));
int power = n - (int)d;
double magnitude = std::pow(10., power);
long shifted = ::round(num*magnitude);
std::ostringstream oss;
oss << shifted/magnitude;
return oss.str();
}
int main() {
std::cout << toPrecision(123.4567, 2) << "\n";
std::cout << toPrecision(123.4567, 4) << "\n";
std::cout << toPrecision(123.4567, 5) << "\n";
}

Check out setprecision() in iomanip. That should do what you are looking for on the double, then just convert to string

Print it to an ostringstream, setting the floating-point formatting parameters as appropriate.

The method above with a ceil(log10(x)) is perfectly legit to determine the number of digit of the integer part of a number. But I feel it's a bit heavy on CPU to call for maths functions just to set a number of digit.
Isn't that simpler to convert the floating value into a string with too many digits, then to work on the string itself?
Here is what I'd try (with Qt instead of the STL):
QString toPrecision(double num, int n) {
QString baseString = QString::number(num, 'f', n);
int pointPosition=baseString.indexOf(QStringLiteral("."));
// If there is a decimal point that will appear in the final result
if (pointPosition != -1 && pointPosition < n)
++n ; // then the string ends up being n+1 in length
if (baseString.count() > n) {
if (pointPosition < n) {
baseString.truncate(n);
} else {
baseString.truncate(pointPosition);
for (int i = n ; i < baseString.count() ; ++i)
baseString[i]='0';
}
} else if (baseString.count() < n) {
if (pointPosition != -1) {
for (int i = n ; i < baseString.count() ; ++i)
baseString.append('0');
} else {
baseString.append(' ');
}
}
return baseString ;
}
But the question was about the STL.. So, let's rewrite it that way:
std::string toPrecision(double num, size_t n) {
std::ostringstream ss;
ss << num ;
std::string baseString(ss.str());
size_t pointPosition=baseString.find('.');
if (pointPosition != std::string::npos && pointPosition < n)
++n ;
if (baseString.length() > n) {
if (pointPosition < n) {
baseString.resize(n);
} else {
baseString.resize(pointPosition);
for (size_t i = n ; i < baseString.length() ; ++i)
baseString[i]='0';
}
} else if (baseString.length() < n) {
if (pointPosition != std::string::npos) {
baseString.append(n-baseString.length(),'0');
} else {
baseString.append(n-baseString.length(),' ');
}
}
return baseString ;
}

Related

Find prefix within a string

I'm currently doing a leetcode question where I have to find a prefix within a sentence and return the word number within the sentence else return -1. I came up with a solution but it crashes with some strings and i dont know why. An example of this is the following:
Input: sentence = "i love eating burger", searchWord = "burg"
Output: 4 (I also get an output of 4)
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
but fails this example:
Input: sentence = "this problem is an easy problem", searchWord = "pro"
Output: 2 ( I get an output of 6)
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
My cout for this one produced a very weird snippet:
problem is an easy problem
problem is an easy problem
problem is an easy problem
problem is an easy problem
probl
proble
problem
problem
problem i
problem is
it completely ignored the first couple substrings when i increments, this is the only time it happens tho.
int isPrefixOfWord(string sentence, string searchWord)
{
string sub;
int count = 1;
for (int i = 0; i < sentence.length(); i++)
{
if (sentence[i] == ' ')
count++;
for (int j = i; j < sentence.length(); j++)
{
sub = sentence.substr(i, j);
cout<<sub<<endl;
if (sub == searchWord)
{
return count;
}
}
}
return -1;
}
Any Ideas?
int isPrefixOfWord(string sentence, string searchWord)
{
string sub;
int count = 1;
for (int i = 0; i < sentence.length() - searchWord.length() - 1; i++)
{
if (sentence[i] == ' ')
count++;
sub = sentence.substr(i,searchWord.length());
if ( sub == searchWord && (sentence[i-1] == ' ' || i == 0))
{
return count;
}
}
return -1;
}
A very simple C++20 solution using starts_with:
#include <string>
#include <sstream>
#include <iostream>
int isPrefixOfWord(std::string sentence, std::string searchWord)
{
int count = 1;
std::istringstream strm(sentence);
std::string word;
while (strm >> word)
{
if ( word.starts_with(searchWord) )
return count;
++count;
}
return -1;
}
int main()
{
std::cout << isPrefixOfWord("i love eating burger", "burg") << "\n";
std::cout << isPrefixOfWord("this problem is an easy problem", "pro") << "\n";
std::cout << isPrefixOfWord("this problem is an easy problem", "lo");
}
Output:
4
2
-1
Currently, LeetCode and many other of the online coding sites do not support C++20, thus this code will not compile successfully on those online platforms.
Therefore, here is a live example using a C++20 compiler
We can just use std::basic_stringstream for solving this problem. This'll pass through:
// The following block might slightly improve the execution time;
// Can be removed;
static const auto __optimize__ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return 0;
}();
// Most of headers are already included;
// Can be removed;
#include <cstdint>
#include <string>
#include <sstream>
static const struct Solution {
static const int isPrefixOfWord(
const std::string sentence,
const std::string_view search_word
) {
std::basic_stringstream stream_sentence(sentence);
std::size_t index = 1;
std::string word;
while (stream_sentence >> word) {
if (!word.find(search_word)) {
return index;
}
++index;
}
return -1;
}
};
The bug that effects the function output is that you aren't handling the increment of i within your inner for loop:
for (int i = 0; i < sentence.length(); i++)
{
if (sentence[i] == ' ')
count++;
for (int j = i; j < sentence.length(); j++)
{
sub = sentence.substr(i, j);
cout<<sub<<endl;
if (sub == searchWord)
{
return count;
}
}
}
Notice that once your inner-loop is complete that i always iterates by one. So your next search through a word will incorrectly start at its next character, which incorrectly searches for "sub-words" instead of only prefixes, and so creates false positives (and unnecessary work).
Also note that every time that you do:
(sub == searchWord)
That this checks all j characters, even though we're only interested in whether the new jth character is a match.
Another bug, which effects your performance and your couts is that you're not handling mismatches:
if (sub == searchWord)
...is never false, so the only way to exit the inner loop is to keep increments j till the end of the array, so sub ends up being large.
A way to fix your second bug is to replace your inner loop like so:
if (sentence.substr(i, i + searchWord.length()) == searchWord)
return count;
and finally, to fix all bugs:
int isPrefixOfWord (const string & sentence, const string & searchWord)
{
if (sentence.length() < searchWord.length())
return -1;
const size_t i_max = sentence.length() - searchWord.length();
for (size_t i = 0, count = 1; ; ++count)
{
// flush spaces:
while (sentence[i] == ' ')
{
if (i >= i_max)
return -1;
++i;
}
if (sentence.substr(i, searchWord.length()) == searchWord)
return count;
// flush word:
while (sentence[i] != ' ')
{
if (i >= i_max)
return -1;
++i;
}
}
return -1;
}
Note that substr provides a copy of the object (it's not just a wrapper around a string), so this takes linear time with respect to searchWord.length(), which is particularly bad the word within sentence is smaller.
We can improve the speed by replacing
if (sentence.substr(i, searchWord.length()) == searchWord)
return count;
...with
for (size_t j = 0; sentence[i] == searchWord[j]; )
{
++j;
if (j == searchWord.size())
return count;
++i;
}
Others have shown nice applications of the libraries that help solve the problem.
If you don't have access to those libraries for your assignment, or if you just want to learn how you could modularise a problem like this without loosing efficiency, then here's a way to do it in c++11 without any libraries (except string):
bool IsSpace (char c)
{
return c == ' ';
}
bool NotSpace (char c)
{
return c != ' ';
}
class PrefixFind
{
using CharChecker = bool (*)(char);
template <CharChecker Condition>
void FlushWhile ()
{
while ((m_index < sentence.size())
&& Condition(sentence[m_index]))
++m_index;
}
void FlushWhiteSpaces ()
{
FlushWhile<IsSpace>();
}
void FlushToNextWord ()
{
FlushWhile<NotSpace>();
FlushWhile<IsSpace>();
}
bool PrefixMatch ()
{
// SearchOngoing() must equal `true`
size_t j = 0;
while (sentence[m_index] == search_prefix[j])
{
++j;
if (j == search_prefix.size())
return true;
++m_index;
}
return false;
}
bool SearchOngoing () const
{
return m_index + search_prefix.size() <= sentence.size();
}
const std::string & sentence;
const std::string & search_prefix;
size_t m_index;
public:
PrefixFind (const std::string & s, const std::string & sw)
: sentence(s),
search_prefix(sw)
{}
int FirstMatchingWord ()
{
const int NO_MATCHES = -1;
if (!search_prefix.length())
return NO_MATCHES;
m_index = 0;
FlushWhiteSpaces();
for (int n = 1; SearchOngoing(); ++n)
{
if (PrefixMatch())
return n;
FlushToNextWord();
}
return NO_MATCHES;
}
};
In terms of speed: If we consider the length of sentence to be m, and the length of searchWord to be n, then original (buggy) code had O(n*m^2) time complexity. But with this improvement we get O(m).

Is there an easy way to convert a vector int set to a string output?

I'm attempting to jump into code again and doing what I can to teach myself the ins and outs of C++. I have the following code that I have been studying and needed to adapt it to a project where I find the factorial decomposition of a number.
The code seems to work and I can output it to the console but I want to process it into a string variable and return that but the main issue I have is converting it from a vector (int , int) to a std::string variable.
I've tried adding the variables and casting them to the string but everytime I do, I seem to lose data.
struct factor {
long prime;
long power;
};
std::string decomp(int n) {
std::vector<factor> result;
for (long i = 2; i <= n; ++i) {
auto prime = true;
for (auto &f : result) {
for (auto r = i; r % f.prime == 0; r /= f.prime) {
prime = false;
++f.power;
}
}
if (prime) {
result.push_back({ i, 1 });
}
}
for (auto& f : result)
{
std::cout << f.prime;
if (f.power > 1) {
std::cout << "^" << f.power;
}
std::cout << " ";
}
}
I have it formatted where the first is the prime number and the second is the power of that prime. I want it to display say, (3, 7) as 3^7 in my string with a space between all new primes such as 3^7 5^3 etc....
Like Bob__ said, you can use std::stringstream and use it as a drop-in replacement for std::cout.
#include <sstream>
std::string to_string(std::vector<factor> const& decomp)
{
std::stringstream ss;
for (auto const& f : decomp)
{
ss << f.prime;
if (f.power > 1)
ss << '^' << f.power;
ss << ' ';
}
return ss.str();
}
As a side note: I suggest you split the decomposition and string conversion in two seperate functions:

Palindromic Integer Checker

One of our assignments in working with C++ in 1st year programming was to write a function that can check if a number is palindromic or not (e.g 16461). I'd upload my code but I don't even know where to begin when it comes to extracting digits from an integer or checking the number of digits in an integer. Any help or hints would be appreciated!
There are many ways to solve this. I like most the solution that builds the mirror number and checks whether it is identical to the original (even though, it is arguably not the most efficient way). The code should be something like:
bool isPalindrom(int n) {
int original = n;
int mirror = 0;
while (n) {
mirror = mirror * 10 + n % 10;
n /= 10;
}
return mirror == original;
}
You can use modulo arithmetic (% operator) to extract individual digits. An alternative would be to get the string representation of your number and work with that.
Hints:
"Number of digits" is a tricky thing to define, since you can always add 0's on the left-hand side and still have the same number. Read carefully and think carefully about how you want to define this.
The digits of an integer are associated with powers of 10 (recall 123 = 1*100 + 2*10 + 3*1), so to extract digits you need to be extracting powers of 10. The key operations are remainder and truncated division. What happens if you do (123 % 10)? How about (123 / 10)? How about ((123 / 10) % 10)?
Best convert the integer into a string first. Testing a string if it is a palindrome is much easier.
#include <sstream>
#include <iostream>
#include <string>
bool isPalindrome(int value)
{
// convert integer into text
std::ostringstream oss;
oss << value;
std::string valueText = oss.str();
if (valueText.size()%2==0) {
return false;
}
for (int i = 0; i < (valueText.size()/2); ++i) {
if (valueText[i]!=valueText[valueText.size()-i-1]) {
return false;
}
}
return true;
}
int main()
{
for (int i = 0; i < 100000; ++i) {
if (isPalindrome(i)) {
std::cout << i << std::endl;
}
}
return 0;
}
First convert the integer into a std::string:
std::ostringstream oss;
oss << value;
std::string valueText = oss.str();
Now check if the string has a odd number of digits:
if (valueText.size()%2==0) {
return false;
}
If the string has a odd number of digits, test if the digits match:
for (int i = 0; i < (valueText.size()/2); ++i) {
if (valueText[i]!=valueText[valueText.size()-i-1]) {
return false;
}
}
Here's a solution that converts the integer to a C-style string and go from there.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool isPalindrome(int n) {
char s[256];
sprintf(s, "%d", n);
char *p = s, *q = &s[strlen(s) - 1];
while (*p == *q && p++ < q--)
;
return (p == q) || (*p == *q);
}
int main() {
int n = 1644451;
cout << isPalindrome(n) << endl;
return 0;
}
If performance is not an issue, and if you can do c++11, an easy to read solution :
template<class T>
bool isPalindrome(T i)
{
auto s = std::to_string(i);
auto s2 = s;
std::reverse(s.begin(), s.end());
return s == s2;
}
which is of course much slower than manually going through the digits, but imho is very readable...
call it with:
std::cout << isPalindrome<int>(12321);
std::cout << isPalindrome<int>(1232);

Input String where Integer should be - C++

I'm a beginner and am stuck on such a simple problem whilst working through Stroustrup's Principles and Practices.
Using only basic elements
#include "std_lib_facilities.h"
int main()
{
double highest = 0;
double lowest = 100;
int i=0;
double sum = 0;
vector <double> inputlist;
double input;
string unit;
cout<<"Type in a number followed by it's unit \n";
while(cin>>input>>unit){
inputlist.push_back(input);
sum += inputlist[i];
if (input >= lowest && input <= highest){
cout<<input<<" \n";
++i;
}
else if (input < lowest){
lowest = input;
cout<<"\nLowest Number so far \n"<<lowest;
++i;
}
else if (input > highest){
highest = input;
cout<<"\nHighest number so far \n"<< highest;
++i;
}
else
cout<<"Lowest is: \n"<<lowest<<"\n\n Highest is: \n"<<highest<<" \n\n and the total is: \n"<<sum;
if (unit == "ft", "m", "in","cm")
cout<<unit<<"\n";
else
cout<<"cannot recognize unit";
}
keep_window_open();
return 0;
}
I need the program to show the user the sum and highest and lowest value when the character "|" is entered. Problem is: i need this entered where the Integer value should be entered.
NOTE: I don't know much about conversions but tried a few and they didn't work.
If I understood you correctly, you want to read int from std::cin, but:
int i;
if (std::cin >> i) {
...
doesn't suite your needs since there might be '|' sign as a signal for termination of reading.
Here's what you could do: read input word by word (std::string) and parse these words separately using temporary std::istringstream:
std::string word;
if (std::cin >> word) {
if (word == "|")
...
// else:
std::istringstream is(word);
int i;
if (is >> i) {
// integer successfully retrieved from stream
}
}
just #include <sstream>
Read the value with string. if it doesn't match | convert it to double using the following function:
double toDouble(string s)
{
int sign = 1, i=0;
if (s[0]=='-')
sign = -1, i=1;
double result = 0, result2 = 0;
for (; i < s.size(); i++)
if (s[i] == '.')
break;
else
result = result * 10 + (s[i] - '0');
for (i = s.size()-1 ; i>=0 ; i--)
if (s[i] == '.')
break;
else
result2 = result2 / 10 + (s[i] - '0');
if (i>=0)
result += result2/10;
return result * sign;
}
Summing meters with inches does not make much sense. Therefore, you should consider to translate the units into scaling factors. You could use a map to get the scaling factors.
Even if this is somewhat overshoot you might use regular expressions to parse the user input. If the regex does not match you can test for stuff like "|".
In the new c++-standard (http://en.wikipedia.org/wiki/C%2B%2B11) a regex library is defined for this purpose. Pityingly, the g++ regex library is buggy. But you can use boost (http://www.boost.org/doc/libs/1_54_0/libs/regex/doc/html/boost_regex/).
Here is an example:
#include <iostream>
#include <vector>
#include <map>
#include <boost/regex.hpp> //< Pittyingly std::regex is buggy.
using namespace std; ///< Avoid this in larger projects!
using namespace boost;
int main() {
const string strReFloat("([-+]?[[:digit:]]*\\.?[[:digit:]]+(?:[eE][-+]?[[:digit:]]+)?)");
const string strReUnit("([[:alpha:]]+)");
const string strReMaybeBlanks("[[:blank:]]*");
const string strReFloatWithUnit(strReMaybeBlanks+strReFloat+strReMaybeBlanks+strReUnit+strReMaybeBlanks);
const regex reFloatWithUnit(strReFloatWithUnit);
const map<const string,double> unitVal= {
{"m", 1.0},
{"in", 0.0254},
{"ft", 0.3048},
{"cm", 0.01}
};
double highest = 0;
double lowest = 100;
int i=0;
double sum = 0;
vector <double> inputlist;
double input;
double unitToMeter;
string unit;
string str;
while( (cout<<"\nType in a number followed by it's unit \n", getline(cin,str), str != "") ){
smatch parts;
if( regex_match(str,parts,reFloatWithUnit) ) {
unit = parts[2].str();
auto found = unitVal.find(unit);
if( found != unitVal.end() ) {
cout<<unit<<"\n";
input = found->second * atof(parts[1].str().c_str());
} else {
cout << "Unit \"" << unit << "\" not recognized. Using meters.\n";
}
inputlist.push_back(input);
sum += inputlist[i];
if (input >= lowest && input <= highest){
cout<<input<<" \n";
++i;
}
else if (input < lowest){
lowest = input;
cout<<"\nLowest Number so far \n"<<lowest;
++i;
}
else if (input > highest){
highest = input;
cout<<"\nHighest number so far \n"<< highest;
++i;
}
} else if( str == "|" ) {
cout << "sum:" << sum << "\n";
} else {
cout << "Input not recognized.\n";
}
}
return 0;
}

Return all prime number before a given number(Homework C++)

I'm not trying to ask you guys to help me to do homework because i've do much research and also try to program it myself but still i encounter problem and i think so far i've know where the problem is but still no solution can be figure out by me :
The Code
#include <iostream>
#include <string>
#include <cmath>
int main(void)
{
using namespace std;
int num;
int max;
string answer = "";
cin >> num;
for(int i = 2 ; i < num ; i++)
{
max = sqrt(i);
if(max < 2) // This must be done beacuse sqrt(2) and sqrt(3)
{ // is 1 which will make it become nonprime.
answer += i;
answer += ' ';
continue;
}
for(int j = 2 ; j <= max ; j++) // Trial division ,divide each by integer
{ // more than 1 and less than sqrt(oftheinteger)
if(i % j == 0)
break;
else if(j == max)
{
answer += i + " ";
answer += ' ';
}
}
}
cout <<"The answer is " << answer ;
return 0;
}
The Question
1.)This program will prompt for a number from user and return all the prime number before it(e.g if user input 9 : then the answer is 2 , 3 , 5 , 7).
2.)I think the wrong part is the string and integer concatenation , till now i still puzzle how to concat string and integer in C++(Previous Javascript programmer so i'm accustomed to using + as string-int concat operator)
3.)Beside the problem i mention above , so far i've go through the code and find none of other problem exist.If any expert manage to find any , mind to point it out to enlighten me??
4.)If there's any mistake in terms of coding or algorithm or anything done by me , please don't hesitate to point it out , i'm willing to learn.
Thanks for spending time reading my question
The usual way to perform formatting in C++ is to use streams.
In this situation, you can use a std::stringstream to accumulate the results, and then convert it into a string when you do the final printing.
Include sstream to get the required type and function declarations:
#include <sstream>
Declare answer to be a std::stringstream instead of a std::string:
stringstream answer;
and then wherever you have:
answer += bla;
, replace it with:
answer << bla;
To get a std::string out of answer, use answer.str():
cout << "The answer is " << answer.str();
If you have to store your complete output before printing it out (I would probably print it as I go, but up to you), a simple way is to use stringstreams.
In this case, rather than answer being an std::string, we can change it to an std::stringstream (and include the <sstream> header).
Now rather than having:
answer += i;
We can just make a simple change and have:
answer << i;
Just as you would if you were printing to cout (which is an ostream).
So basically, += in your code would become <<.
Similar to printing to cout, you can also chain together such as:
answer << a << b
Now to print your stringstream to cout, all you'd need to do is:
cout << my_stringstream.str()
See how you go. I don't want to provide you with the complete since it's homework.
You can go around the string concatenation problem if you just print what you have so far:
int main()
{
int num;
int max;
string answer = "";
cin >> num;
cout << "The answer is ";
for(int i = 2 ; i < num ; i++)
{
max = sqrt((double)i);
if(max < 2) // This must be done beacuse sqrt(2) and sqrt(3)
{ // is 1 which will make it become nonprime.
cout << i << ' ';
continue;
}
for(int j = 2 ; j <= max ; j++) // Trial division ,divide each by integer
{ // more than 1 and less than sqrt(oftheinteger)
if(i % j == 0)
break;
else if(j == max)
{
cout << i << ' ';
}
}
}
return 0;
}
As other mentioned one way to do concatenation is std::stringstream.
it's not very beautiful, but it works. I use a general library "genlib.h", I'm not sure what you use, so you might need to replace that or I can send it to you.
#include "genlib.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
bool IsPrime(int num);
int main()
{
int num;
int i = 2;
cout << "Enter an integer to print previous primes up to: ";
cin >> num;
cout << endl << "The primes numbers are: " << endl;
while(i < num){
if (IsPrime(i) == true){
cout << i << ", ";
}
i++;
}
return 0;
}
bool IsPrime(int num){
if((num == 2) || (num == 3)) {
return true;
}else if ((num % 2) == 0){
return false;
}else{
for (int i = 3; i < sqrt(double(num))+1; i++){
if ((num % i) == 0){
return false;
}
return true;
}
}
}
you need tn convert the integer to string (char*, exactly) using :
answer += itoa(i);
or using standard function :
char str[10];
sprintf(str,"%d",i);
answer += str;
and if you want to avoid using sqrt function, you can replace :
for(int i = 2 ; i < num ; i++)
{
max = sqrt(i);
with :
for(int i = 2 ; i*i < num ; i++)
{
The problem is that the + operator of std::string accepts strings as parameter, pointers to an array of chars, or single chars.
When a single char is used in the + operator, then a single char is added to the end of the string.
Your C++ compiler converts the integer to char before passing it to the operator + (both char and int are signed integer values, with different bit number), and therefore your string should contain a strange char instead of the numbers.
You should explicitly convert the integer to string before adding it to the string, as suggested in other answers, or just output everything to std::cout (its operator << accepts also int as parameter and convert them correctly to string).
As a side note, you should receive a warning from the C++ compiler that your integer i has been converted to char when you add it to the string (the integer has been converted to a lower resolution or something like that). This is why is always good to set the warning level to high and try to produce applications that don't generate any warning during the compilation.
You could perform a faster lookup by storing your known prime numbers in a set. These two sample functions should do the trick:
#include <iostream>
#include <set>
#include <sstream>
#include <string>
typedef std::set< unsigned int > PrimeNumbers;
bool isComposite(unsigned int n, const PrimeNumbers& knownPrimeNumbers)
{
PrimeNumbers::const_iterator itEnd = knownPrimeNumbers.end();
for (PrimeNumbers::const_iterator it = knownPrimeNumbers.begin();
it != itEnd; ++it)
{
if (n % *it == 0)
return true;
}
return false;
}
void findPrimeNumbers(unsigned int n, PrimeNumbers& primeNumbers)
{
for (unsigned int i = 2; i <= n; ++i)
{
if (!isComposite(i, primeNumbers))
primeNumbers.insert(i);
}
}
You could then invoke findPrimeNumbers like so:
unsigned int n;
std::cout << "n? ";
std::cin >> n;
PrimeNumbers primeNumbers;
findPrimeNumbers(n, primeNumbers);
And if you really need to dump the result in a string:
std::stringstream stringStream;
int i = 0;
PrimeNumbers::const_iterator itEnd = primeNumbers.end();
for (PrimeNumbers::const_iterator it = primeNumbers.begin();
it != itEnd; ++it, ++i)
{
stringStream << *it;
if (i < primeNumbers.size() - 1)
stringStream << ", ";
}
std::cout << stringStream.str() << std::endl;
Since you're willing to learn, you can perform both join and split algorithm on string/sequence by using Boost String Algorithms Library.
This solution is not perfect, but it's basic C++ usage (simple containers, no structure, only one typedef, ...).
Feel free to compare your results with The First 1000 Primes.
Good luck