Hello Everyone,
This is the following code of a package:
for (int iter = 0; iter < flags.total_iterations_; ++iter) {
std::cout << "Iteration " << iter << " ...\n";
if (flags.compute_likelihood_ == "true") {
double loglikelihood = 0;
for (list<LDADocument*>::const_iterator iterator = corpus.begin();
iterator != corpus.end();
++iterator) {
loglikelihood += sampler.LogLikelihood(*iterator);
}
std::cout << "Loglikelihood: " << loglikelihood << std::endl;
}
sampler.DoIteration(&corpus, true, iter < flags.burn_in_iterations_);
}
accum_model.AverageModel(
flags.total_iterations_ - flags.burn_in_iterations_);
FreeCorpus(&corpus);
std::ofstream fout(flags.model_file_.c_str());
accum_model.AppendAsString(word_index_map, fout);
return 0;
I would like to tweak this in such a way that for every 20 iterations, I would like to write a file that stores the result of fout. I am actually beginner in code coding in python. Since the package has the codes in c++, I have no idea what to input where.
I understand the logic like:
There must be a counter which counts the iteration and for every 20th iteration, a file must be created and the result of fout must be saved in that file. And for every 20th iteration I need new files to be created as I do not want the contents to be overwritten for analysis purpose.
Please help me as am a newbie and totally clueless about c++. Thanks in advance!
Try this:
for (int iter = 0; iter < flags.total_iterations_; ++iter) {
double loglikelihood = 0;
std::cout << "Iteration " << iter << " ...\n";
if(iter%20==0) {
const char *path1 = "Your path to the files"
std::ofstream llh_file;
std::ofstream myfile;
std::string result;
char numstr[30];
sprintf(numstr, "%d", iter);
result = path1 + std::string (numstr) + ".txt";
myfile.open(result.c_str());
model.AppendAsString(myfile);
myfile.close();
Related
I have the following simple code. I declare a vector and initialize it with one value 21 in this case. And then i am trying to find that value in the vector using find. I can see that the element "21" in this case is in the vector since i print it in the for loop. However why the iterator of find does not resolve to true?
vector<uint8_t> v = { 21 };
uint8_t valueToSearch = 21;
for (vector<uint8_t>::const_iterator i = v.begin(); i != v.end(); ++i){
cout << unsigned(*i) << ' ' << endl;
}
auto it = find(v.begin(), v.end(), valueToSearch);
if ( it != v.end() )
{
string m = "valueToSearch was found in the vector " + valueToSearch;
cout << m << endl;
}
are you sure it doesn't work?
I just tried it:
#include<iostream> // std::cout
#include<vector>
#include <algorithm>
using namespace std;
int main()
{
vector<uint8_t> v = { 21 };
uint8_t valueToSearch = 21;
for (vector<uint8_t>::const_iterator i = v.begin(); i != v.end(); ++i){
cout << unsigned(*i) << ' ' << endl;
}
auto it = find(v.begin(), v.end(), valueToSearch);
if ( it != v.end() )
{// if we hit this condition, we found the element
string error = "valueToSearch was found in the vector ";
cout << error << int(valueToSearch) << endl;
}
return 0;
}
There are two small modifications:
in the last lines inside the "if", because you cannot add directly a
number to a string:
string m = "valueToSearch was found in the vector " + valueToSearch;
and it prints:
21
valueToSearch was found in the vector 21
while it's true that you cannot add a number to a string, cout
support the insertion operator (<<) for int types, but not uint8_t,
so you need to convert it to it.
cout << error << int(valueToSearch) << endl;
This to say that the find is working correctly, and it is telling you that it found the number in the first position, and for this, it != end (end is not a valid element, but is a valid iterator that marks the end of your container.)
Try it here
I've started a little toy project as a way to teach myself C++ and have hit a wall. In the code below the highlighted line is causing a build error saying:
welcome.cc:65:26: error: cannot convert
'std::_Rb_tree_iterator >' to 'char' in
assignment
current = result.first;
and I'm not sure why. The goal is to create a Markov chain for English placenames, the intent of the line in question is meant to update the current letter in the generated placename with a randomly selected value of the previous one, my random selection method is taken from this question. As I say, new to C++ and no idea what I've done wrong.
int main(int argc, char**argv) {
string line;
ifstream myfile;
std::multimap<char, char> m;
myfile.open ("C:\\Users\\james\\Desktop\\placenames.txt");
if (myfile.is_open()){
while ( getline (myfile,line) )
{
for(std::string::size_type i = 0; i < line.size(); ++i) {
std::cout<<line[i]<<std::endl;
m.insert(std::pair<char, char>(line[i], line[i+1]));
}
}
for (std::multimap<char, char>::iterator it = m.begin();it != m.end(); ++it)
cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
myfile.close();
// for( auto it = m.begin(), end = m.end();it != end; it = m.upper_bound(it->first))
// cout << (*it).first << ' ' << m.count( (*it).first) << endl;
auto current = 'A';
std::string name = "A";
typedef std::multimap<const char, char>::iterator MMAPIterator;
for (int j=0; j<8; ++j){
std::pair<MMAPIterator, MMAPIterator> result = m.equal_range(current);
std::size_t sz = std::distance(result.first, result.second);
std::size_t idx = std::rand() % sz;
std::advance(result.first, idx);
current = result.first; <------THIS LINE
//name+=current;
}
cout << name;
}
else cout << "Unable to open file";
return 0;
}
I'm aware its a pretty naive implementation of a markov chain, btw. As I say, intent was to play around with c++ rather than anything else.
You have a dual layer of pairs here, firstly the equal_range function returns a range, so your
std::size_t sz = std::distance(result.first, result.second);
Tells you how many items are in the range, to get an element of that range (your pair of chars) you need to
auto random_element = result.first;
auto the_const_char = random_element.first;
auto the_non_const_char = random_element.second;
I am supposed to read some data (specifically string datatype) and store each element in a vector. Now I have to check if any of the different strings that were inputted match in size, and if they do I have to see if there are any matching letters. Now my question is how do I compare what's inside the vector (first the size and then the different letters). Is it possible?
Say I have
HELLO
and
HELLA
They have the same size, and 4 letters that match.
This is what I am trying to accomplish.
The code that I have does not work given my ignorance about the matter.
Thank you in advance.
vector <string> myVector;
//insert data insdide of the vector
myVector.push_back("Hello");
myVector.push_back("Hello");
myVector.push_back("Hello2");
myVector.push_back("Hello3");
myVector.push_back("Hello4");
//This is wrong
for (unsigned int i = 0; i < myVector.size(); i++) {
if (myVector[i].size == myVector[i+1].size()){
cout << "SAME SIZE" << endl;
}
}
return 0;
You just have make a simple mistake for size() function and you are trying to access the element which is not present by using i+1 for last iteration.
So just change your for loop just as below
for (unsigned int i = 1; i < myVector.size(); i++)
{
if (myVector[i].size() == myVector[i-1].size()) // .size() should be used
{
cout << "SAME SIZE" << endl;
}
}
Here's a way of writing it:
// returns true if #param s1 and #param s2 are equal in letters
bool isEqual(const string& s1, const string& s2) {
if(s1.size() != s2.size())
return false;
bool equal = false;
// iterates over all the characters in s1 and s2 and compare them
for(auto ch1 = s1.cbegin(), ch2 = s2.cbegin(); ch1 != s1.cend(),ch2!= s2.cend(); ch1++, ch2++) {
if(*ch1 == *ch2)
equal = true;
else
return false;
}
return equal;
}
// type of iter is vector<string>::const_iterator meaning it can only read the value
for (auto iter = myVector.cbegin(); iter != myVector.cend() - 1; iter++){
if(isEqual(*iter, *(iter + 1)))
std::cout << *iter << " equal " << *(iter + 1) << endl;
else
std::cout << *iter << " different " << *(iter + 1) << endl;
}
Here, I used iterators(you should write code in modern C++, avoid using subscript).
I'm trying to iterate through a map to read out a string and then all of the numbers in a vector to a file. I copied and pasted the typedef line, then adjusted it to my code, so I'm not positive it's correct. Anyways, Visual Studio is giving me errors on the use of iterator_variable in my loops. It says type name is not allowed. How can I fix this?
ofstream output("output.txt");
typedef map<string, vector<int>>::iterator iterator_variable;
for (iterator_variable iterator = misspelled_words.begin(); iterator != misspelled_words.end(); iterator++)
{
output << iterator_variable->first;
for (int i = 0; i < misspelled_words.size(); i++)
{
output << " " << iterator_variable->second[i];
}
output << endl;
}
You should access the iterator like iterator->first instead of iterator_variable->first.
And for the inner loop, you probably want to iterate through 0 to iterator->second.size() instead of misspelled_words.size().
ofstream output("output.txt");
typedef map<string, vector<int>>::iterator iterator_variable;
for (iterator_variable iterator = misspelled_words.begin(); iterator != misspelled_words.end(); iterator++)
{
output << iterator->first;
for (int i = 0; i < iterator->second.size(); i++)
{
output << " " << iterator->second[i];
}
output << endl;
}
You can use the the new range based for loop and auto for more concise and readable code too.
ofstream output("output.txt");
for ( auto const & ref: misspelled_words ) {
output << ref.first;
for (auto const & ref2 : ref.second ) {
output << " " << ref2;
}
output << "\n"; // endl force a stream flush and slow down things.
}
I have a std::set of strings and I want to iterate over them, but the iterator is behaving differently for different sizes of set. Given below is the code snippet that I'm working on:
int test(set<string> &KeywordsDictionary){
int keyword_len = 0;
string word;
set<string>::iterator iter;
cout << "total words in the database : " << KeywordsDictionary.size() << endl;
for(iter=KeywordsDictionary.begin();iter != KeywordsDictionary.end();iter++) {
cout << *iter;
word = *iter;
keyword_len = word.size();
if(keyword_len>0)
Dosomething();
else
cout << "Length of keyword is <= 0" << endl;
}
cout << "exiting test program" << endl;
}
The code is working properly & *iter is being dereferenced & assigned to word until the size of KeywordsDictionary is around 15000. However when the size of KeywordsDictionary increases beyond 15000,
the print statement cout << *iter; is printing all the contents of KeywordsDictionary correctly.
but the pointer to the iterator *iter is not being dereferenced & not being assigned to word. word is just being an empty string.
EDIT: And the output of the program is :
total words in the database : 22771
�z���AAAADAAIIABABBABLEABNABOUTACACCEPTEDACCESSACCOUNT...
Length of keyword is <= 0
exiting test program
So basically, I'm guessing the loop is executing only once.
Try to declare keyword_len as
std::string::size_type keyword_len = 0;
instead of
int keyword_len = 0;