How to read utf8 text file line by line?
I want to read some utf8 text files line by line in c++ using std::codecvt_utf8 facet and getline function, on a windows machine. My program works fine for some files but is failed for some others. For those failed ones, my program can read only one single line, but they can be loaded in Sublime successfully with correct number of lines.
I have tried the method mentioned in another stackoverflow answer . The code is as following:
std::wifstream wif(filename);
wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
std::wstringstream wss;
wss << wif.rdbuf();
std::wstring wstr;
int num = 0;
while (std::getline(wss, wstr))
{
num++;
}
std::cout << "Number of lines: " << num << std::endl;
Runnning the above code on this text file . I expected the output to be 3, but the actual output is 1.
PS: the question is marked as duplicated with (Read file line by line using ifstream in C++) . However the answers there won't work for this case.
See also questions close to this topic

I'm receiving a template error and I'm not sure why
I was tasked with the following assignment. The requirements (simplified) are ... Using STL containers std::list, std::vector, and std::deque, demonstrate the sorting of integers in descending order. Use std::set to create a data set of unique integers. Copy the data set to the other containers. Shuffle the containers. Display each container's contents. Use appropriate sorting algorithms to sort the data in each container. Redisplay each container's contents. Code must be written in C++
This is the code I came up with but it's throwing an error on line 20 and 30
'greater is not a template'
. I'm sure it's an easy fix that I am missing but everything I try doesn't work. Thanks for the help.
#include "stdafx.h" #include <iostream> #include <set> #include <iterator> #include <deque> #include <list> #include <vector> #include <algorithm> #include <math.h> using namespace std; int main() { set <int, greater <int>> set1; set1.insert(4); set1.insert(5); set1.insert(3); set1.insert(6); set1.insert(2); set1.insert(5); set1.insert(1); set <int, greater <int> > ::iterator itr; cout << "\n/the initial set is : "; for (itr = set1.begin(); itr != set1.end(); ++itr) { cout << '\t' << *itr; } cout << endl; deque <int> deque_ex1; list <int> list_ex1; vector <int> vec_ex1; for (itr = set1.begin(); itr != set1.end(); ++itr) { vec_ex1.push_back(*itr); deque_ex1.push_back(*itr); list_ex1.push_back(*itr); } std::random_shuffle (vec_ex1.begin(), vec_ex1.end()); std::random_shuffle(deque_ex1.begin(), deque_ex1.end()); vector<int> V(list_ex1.begin(), list_ex1.end()); std::random_shuffle(V.begin(), V.end()); list_ex1.assign(V.begin(), V.end()); vector <int> ::iterator itr1; cout << "The shuffled vector is: "; for (itr1 = vec_ex1.begin(); itr1 != vec_ex1.end(); ++itr1) { cout << '\t' << *itr1; } cout << endl; deque <int> ::iterator itr2; cout << "The shuffled deque is: "; for (itr2 = deque_ex1.begin(); itr2 != deque_ex1.end(); ++itr2) { cout << '\t' << *itr2; } cout << endl; list <int> ::iterator itr3; cout << "The shuffled list is: "; for (itr3 = list_ex1.begin(); itr3 != list_ex1.end(); ++itr3) { cout << '\t' << *itr3; } cout << endl; cout << endl; cout << "sorted data structures (using system defined sort function):" << endl; sort(vec_ex1.begin(), vec_ex1.end()); cout << "The sorted vector is :"; for (itr1 = vec_ex1.begin(); itr1 != vec_ex1.end(); ++itr1) { cout << '\t' << *itr1; } cout << endl; sort(deque_ex1.begin(), deque_ex1.end()); cout << "The sorted Deque is: "; for (itr2 = deque_ex1.begin(); itr2 != deque_ex1.end(); ++itr2) { cout << '\t' << *itr2; } cout << endl; list_ex1.sort(); cout << "The sorted list is: "; for (itr3 = list_ex1.begin(); itr3 != list_ex1.end(); ++itr3) { cout << 't' << *itr3; } cout << endl; return 0; }

FloydWarshall modified algorithm
I'm trying to find all the shortest paths between pair of vertices using FloydWarshall algorithm. But, in the original FloydWarshall algorithm, it determines only one shortest path between a pair of vertices, but for my problem, I want to calculate all shortest paths (i.e., equiweighted paths) between every vertex pair.
Based on some earlier answers, I've managed to write the C++ code as follows:
#include<set> #include<vector> #include<map> #include<array> #include<algorithm> using namespace std; typedef vector<vector<set<int> > > fl; fl fw(int n, vector<array<float,4>> edge) { vector<vector<float> > cost(n); vector<vector<set<int> > > next_node(n); for(int i=0;i<n;i++) { next_node[i]=vector<set<int> >(n); cost[i]=vector<float>(n,INT_MAX); cost[i][i]=0; } for(auto &i:edge) { if(i[2]<cost[i[0]][i[1]]) { cost[i[0]][i[1]]=i[2]; next_node[i[0]][i[1]]=set<int>{i[1]}; } else if(i[2]==cost[i[0]][i[1]] && i[0]<INT_MAX) { (next_node[i[0]][i[1]]).insert(i[1]); } } for(int k=0;k<n;k++) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { float cost_ik_kj = cost[i][k]+cost[k][j]; if(cost_ik_kj < cost[i][j]) { cost[i][j]=cost_ik_kj; next_node[i][j]=set<int>{next_node[i][k]}; } else if(cost_ik_kj==cost[i][j] && cost_ik_kj<MAX) { (next_node[i][j]).insert((next_node[i][k]).begin(),(next_node[i][k]).end()) } } } } return next_node; }
But after returning the next_node vector, I'm unsure of how to reconstruct the path from it in C++11. What will be the path_reconstruction function for the same? The input edge to fw function is in the edgelist format i.e.,
vector<vector<float>> edge{{0,2,2},{2,3,2},{3,1,1},{1,0,4},{1,2,3},{0,3,4},{3,0,5}};
For reference: I used the below Python code as reference for writing in above C++ code.
def floyd_warshall(n, edge): rn = range(n) cost = [[inf] * n for i in rn] next_node = [[set() for j in rn] for i in rn] for i in rn: cost[i][i] = 0 for u, v, w in edge: # The data format allows multiple edges between two nodes. if w < cost[u][v]: cost[u][v] = w next_node[u][v] = set([v]) elif w == cost[u][v] and w < inf: next_node[u][v].add(v) for k in rn: for i in rn: for j in rn: cost_ik_kj = cost[i][k] + cost[k][j] if cost_ik_kj < cost[i][j]: cost[i][j] = cost_ik_kj next_node[i][j] = set(next_node[i][k]) # Want a copy. elif cost_ik_kj == cost[i][j] and cost_ik_kj < inf: next_node[i][j].update( next_node[i][k] ) return next_node
The path reconstruction function using next_nodes is an iterator function all_paths:
def all_paths(next_node, i, j): if 0 == len(next_node[i][j]): if i == j: yield [j] else: pass # There is no path. else: for k in next_node[i][j]: for rest in all_paths(next_node, k, j): yield [i] + rest edge = [[0,2,2],[2,3,2],[3,1,1],[1,0,4],[1,2,3],[0,3,4],[3,0,5]] # Here n is the value of the highest numberedvertex. In the above graph, n=4 n=4 next_node = floyd_warshall(n,edge) for i in range(4): for j in range(4): for path in all_paths(next_node, i, j): print((i, j, path))
Edited: If there is a better way of doing it, it's quite welcome :)

What's the difference between SystemParametersInfoA and SystemParametersInfo?
Sorry I don't quite understand the difference between the two. When I set my desktop wallpaper, only the one with the A at the end works. However, the other one doesn't give me an error. What's going on?